Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Contact ID in Mailjet v3 PHP wrapper?

I have used the Mailjet api to store submitted emails in a Mailjet list. This worked properly when there was the Mailjet 0.1 version API (then there wasn't any PHP wrapper but was easy to use with their examples), but when they changed the API to version 3 their PHP wrapper does not return any contact ID when adding a new contact to a contact list. I have raised a similar question earlier how to get Mailjet to work but now this issue arose with the new version 3.

Again here is the corrected code,

*Assume that the shown email is a new contact that's not already created in Mailjet.

$mj = new Mailjet();
$contact_params = array("method" => "POST", "Email" => "[email protected]");
$contact = $mj->contact($contact_params);

$add_params = array(
    "method" => "POST",
    "ContactID" => $contact,
    "ListID" => _MAILJET_LIST,
    "IsActive" => "True"
);
$mj->listrecipient($add_params);

Developer @Gormador said to use $contact->Data[0]->ID as contact id but it still returns NULL.

I get this error when trying to add a created contact to a list (with debug mode set to 2).

array(3) { ["ContactID"]=> NULL ["ListID"]=> string(1) "2"
["IsActive"]=> string(4) "True" } string(105) "{ "ErrorInfo" : "",
"ErrorMessage" : "Invalid data: \"null\" is an invalid integer",
"StatusCode" : 400 }"

Previous question: Mailjet: Adding email to list does not work

Updated: Full code with corrections,

$mj = new Mailjet(); 
$add_email = "testing" . rand(0, 1000) . "@gmail.com"; 
$contact_params = array("method" => "POST", "Email" => $add_email); 
$mj->contact($contact_params); 
$viewContact = array("method" => "VIEW","unique" => $add_email); 
$contact = $mj->contact($viewContact); 
$add_params = array( "method" => "POST", "ContactID" => $contact->Data[0]->ID, "ListID" => 1, "IsActive" => "True" ); 
$mj->listrecipient($add_params);
like image 986
Teshan Nanayakkara Avatar asked May 22 '15 09:05

Teshan Nanayakkara


1 Answers

EDIT:

This issue isn't necessarily specific to the goal you try to achieve.
Although, there definitely is a bug in our 1.0.6 wrapper. In the wrapper code, we use the 4th parameter of the json_decode() function, which was only introduced in PHP 5.4 (See the "Changelog" section here).

Two ways you can solve this:

  • Either update your system's PHP version to >= 5.4
  • Or (until we introduce a fix on our side), modify the wrapper locally to not use that option (see here), which very well might bring new issues and isn't the recommended fix.

Hope this helped. We certainly are grateful that this brought that particular issue to our attention :)

Edit2:

To clarify, here's what the second approach means in term of code:

$this->_response = json_decode($buffer, false, 512, JSON_BIGINT_AS_STRING);
//Becomes
$this->_response = json_decode($buffer, false, 512);

That should eliminate the warning and solve the issue. Isn't that great?! ;-)


As I really am not able to reproduce your issue on my end, here is a question:
Is the contact you are trying to create listed in the response when you issue the following code ?
Alternatively, go here for a list of your account's contacts.

$mj = new Mailjet($apiKey, $secretKey);
$contact = $mj->contact();

var_dump($contact);

If it is, this is why what I wrote in my response on your other question doesn't work.

But why?

Well, you are trying to create a contact that is already listed in your account. The process of creating a contact doesn't necessarily mean that it has to be assigned to a contacts list.

The fix

Either get the existing contact's id instead of creating it before adding it to a list:

$mj = new Mailjet($apiKey, $secretKey);

$viewContact = [
    "method"    =>  "VIEW",
    "unique"    =>  "[email protected]"
];

$contact = $mj->contact($viewContact);

echo "Contact ID: ".$contact->Data[0]->ID."\n";

// Then proceed as described in my original answer.

Or delete it through the web interface and try following my answer again.
(Click on the contact's email, then click on the yellow menu next to its avatar and click on remove.)

Does that solve your issue? :-)

like image 137
Gormador Avatar answered Sep 27 '22 18:09

Gormador