drewm / mailchimp-api

Super-simple, minimum abstraction MailChimp API v3 wrapper, in PHP
MIT License
1.99k stars 505 forks source link

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

Open MayorUshanka opened 3 years ago

MayorUshanka commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

did you find a workaround for this ?

rossjcooper commented 3 years ago

Also getting this issue

rossjcooper commented 3 years ago

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 commented 3 years ago

I've got it working by doing this

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

Which seems to trigger it to encode correctly 🤞

GiGa1989 commented 3 years ago

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 commented 3 years ago

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);
            }
        }
    }
}