How to get contact id in Mailjet v3?

I used the Mailjet api to store sent emails in the Mailjet list. This worked correctly when there was an API version of Mailjet 0.1 (then there was no PHP shell, but it was easy to use with their examples), but when they changed the API to version 3, their PHP shell did not return the contact identifier when adding a new contact in the contact list . Earlier, I asked a similar question how to get Mailjet to work, but now this problem has arisen with the new version 3.

The corrected code here again,

* Assume that the specified email is a new contact that has not yet been created in Mailjet.

$mj = new Mailjet(); $contact_params = array("method" => "POST", "Email" => " abc@gmail.com "); $contact = $mj->contact($contact_params); $add_params = array( "method" => "POST", "ContactID" => $contact, "ListID" => _MAILJET_LIST, "IsActive" => "True" ); $mj->listrecipient($add_params); 

The developer @Gormador said to use $contact->Data[0]->ID as the contact identifier, but it still returns NULL.

I get this error when trying to add the created contact to the list (debug mode is 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 the 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); 
+6
source share
1 answer

EDIT:

This problem is not necessarily specific to the goal you are trying to achieve.
Although, there is definitely a bug in our wrapper 1.0.6. In the shell code, we use the 4th parameter of the json_decode() function, which was introduced only in PHP 5.4 (see the Changelog section here ).

Two ways to solve this problem:

  • Or upgrade your PHP version to> = 5.4
  • Or (until we present a fix on our side), change the shell locally so as not to use this option (see here ) , which can very well bring new problems and is not a recommended fix .

Hope this helps. We are certainly grateful that this attracted attention to this issue :)

Edit2:

To clarify, here is what the second approach means in terms of code:

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

This should fix the warning and resolve the problem. Is not that great?!; -)


Since I really cannot reproduce your problem at my end, here is the question:
Is the contact you are trying to create indicated in the answer when releasing the following code?
Alternatively, go here for your accountโ€™s contact list.

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

If so, that is why what I wrote in the answer to your other question does not work.

But why?

Well, you are trying to create a contact that is already listed in your account. The process of creating a contact does not necessarily mean that it must be assigned to the contact list.

Correction

Or get the existing contact identifier, rather than create it before adding it to the list:

 $mj = new Mailjet($apiKey, $secretKey); $viewContact = [ "method" => "VIEW", "unique" => " gaetan.philippot@gmail.com " ]; $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 repeat my answer again.
(Click on the contact email address, then click on the yellow menu next to his avatar and click on delete.)

Does your problem solve? :-)

+3
source

Source: https://habr.com/ru/post/987727/


All Articles