Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST /lists/list_id/members/sub_id/tags fails: This value should be of type iterable. #305

Open
MayorUshanka opened this issue Jan 8, 2021 · 7 comments

Comments

@MayorUshanka
Copy link

MayorUshanka commented Jan 8, 2021

Mailchimp documentation:

https://mailchimp.com/developer/api/marketing/list-member-tags/add-or-remove-member-tags/

Code:

require("vendor/autoload.php");
use \DrewM\MailChimp\MailChimp;

MailChimp = new MailChimp(SCRUBBED);

// get subscriber hash
$subscriber_hash = md5(trim(strtolower($email)));

// update member's tags
$payload = [
	'tags' => [
		'name' => $tag,
		'status' => 'active'
	]
];

$upd = $MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", $payload);

print_r($upd);

output:

Array ( [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => Invalid Resource [status] => 400 [detail] => The resource submitted could not be validated. For field-specific details, see the 'errors' array. [instance] => SCRUBBED [errors] => Array ( [0] => Array ( [field] => tags [message] => This value should be of type iterable. ) ) ) 

When I double wrap the tags (so it becomes an array of arrays), mailchimp complains that it should be a string. What does mailchimp actually want from me? Is this a bug?

@MayorUshanka
Copy link
Author

When formatting it like this:

$MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", [
	'tags' => ["
		\"name\": \"$tag\",
		\"status\":active
	"]
]);

It seems to be correctly formatted. Except mailschimp now complains that the field "name" is required - even though I clearly filled it in (I even tried hardcoding the $tag).

 [errors] => Array ( [0] => Array ( [field] => tags[0].name [message] => This value should not be blank. ) )

@CNundun
Copy link

CNundun commented Mar 4, 2021

did you find a workaround for this ?

@rossjcooper
Copy link

Also getting this issue

@rossjcooper
Copy link

I believe its due to https://github.com/drewm/mailchimp-api/blob/master/src/MailChimp.php#L390 when it is encoding the array of tags it is creating a JSON object instead.

@rossjcooper
Copy link

I've got it working by doing this

[
    'tags' => array_values($tags),
]

Which seems to trigger it to encode correctly 🤞

@GiGa1989
Copy link

GiGa1989 commented Apr 3, 2021

I tried this snippet but it didn't work. It creates the tag on mailchimp and the user doesn't receive the mail.

$added = $this->mailchimp_api->post('/lists/' . $this->list_id . '/members/' . $email_hash . '/tags', [
  "tags" => array_values([
    [
      "name" => "Influencer",
      "status" => "active"
    ]
  ])
  ]
);

How can I subscribe a user with a specific tag?

@MayorUshanka
Copy link
Author

Ok, here is the code that seems to work for me

class MAILCUCK{ // MAILChimp Utilities Class Kernel
	private $MailChimp;
	private $debug;

	function __construct($debug = false){
		$this->MailChimp = new MailChimp('apikey');
		$this->debug = $debug;
	}

	function subscribe_or_add_tag($list_id, $email, $tag, $merge_fields=[]){

		// we have an audience (list).
		// we want to add an email with a tag to this list
		// if the email is already in the list, just append the tag
		// if not, subscribe along with the tag

		/*
		$merge_fields = [
			'FNAME' => "testF",
			'LNAME' => "testL",
			'ADDRESS' => "addr",
			'PHONE' => "testtel",
			'FULLNAME' => "TestF TestL"
		];
		*/

		$payload;

		if(empty($merge_fields)){
			$payload = [
				'email_address' => $email,
				'status'        => 'subscribed',
				'tags' => [$tag],
			];
		} else {
			$payload = [
				'email_address' => $email,
				'status'        => 'subscribed',
				'tags' => [$tag],
				'merge_fields' => $merge_fields,
			];
		}

		$result = $this->MailChimp->post("lists/$list_id/members", $payload);

		if ($this->debug){
			echo "Result: ".$result;
		}

		if ($result['title'] == "Member Exists"){

			if ($this->debug){
				echo "Member alreay exists, appending tag... ";
			}

			$subscriber_hash = md5(trim(strtolower($email)));

			$payload = [
				'tags'=>[
					[
						"name" => $tag,
						"status" => "active"
					]
				]
			];

			// update member's tags
			$upd = $this->MailChimp->post("lists/$list_id/members/$subscriber_hash/tags", $payload);

			if ($this->debug){
				print_r($upd);
			}
		} else {
			if ($this->debug){
				print_r($result);
			}
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants