Closed JokePortegies closed 9 years ago
Hello,
Could you please provide me the code you use in your workflow to help me identify where the problems comes from? By the way, if you want to update these contact datas, that means that you need to create them previously.
Thanks!
I'm so glad that you want to look into this with me! In Mailjet I addes contacts properties, with the names "voornaam" and "achternaam". I think you mean that by your remark about the creation of the contact data?
This is the code that is relevant to the problem:
/**
* This function is a part of the Mailjet for Wordpress plugin. I added two input-fields to the form
*/
public function mailjet_subscribe_from_widget()
{
// Get some variables - email, list_id, etc.
$email = $_POST['email'];
$list_id = $_POST['list_id'];
// Add the contact to the contact list
$result = $this->api->addContact(array(
'Email' => $email,
'ListID' => $list_id
));
// Check what is the response and display proper message
if(isset($result->Status)) {
if($result->Status == 'DUPLICATE'){
echo '<p class="error">';
echo sprintf(__("The contact %s is already subscribed", 'wp-mailjet'), $email);
echo '</p>';
die();
}
else if($result->Status == 'ERROR'){
echo '<p class="error">';
echo sprintf(__("Sorry %s we couldn't subscribe at this time", 'wp-mailjet'), $email);
echo '</p>';
die();
}
}
// Adding was successful
// retrieve the Contact_id that was added just now
$contact_id = getContact($list_id, $email);
// build the array with the contactmetadata
$data = array(array('Name' => 'achternaam', 'Value' => $_POST['achternaam']), array('Name' => 'voornaam', 'Value' => $_POST['voornaam']));
// update the contactdata
updateContactData($contact_id, $data);
echo '<p class="success">';
echo sprintf(__("Thanks for subscribing with %s", 'wp-mailjet'), $email);
echo '</p>';
die();
}
// These functions are part of my functions.php, in which I included the php-mailjet-v3-simple.class.php
/**
* Mailjet-toevoegingen
*
*/
function updateContactData($id, $data) { $mj = new Mailjet(); $params = array( 'ID' => $id, 'Data' => $data, 'method' => 'PUT' );
$result = $mj->contactdata($params);
if ($mj->_response_code == 200)
echo "success - data changed";
else
// and here I get the 404-error-code
echo "error - ".$mj->_response_code;
return $result;
}
function getContact($list_id, $email) { $mj = new Mailjet(); $params = array( "method" => "GET", "ContactsList" => $list_id, "ContactEmail" => $email, ); $mj->listrecipient($params); return $mj->_response->Data[0]->ID; }
Thanks!
Before updating the metadata for a particular contact, have you created these metadata ("voornaam" and "achternaam") for all of them?
It can be done by POST-ing a contactmetadata
ressource.
You can find how to do it through in our Getting Started guide, in the "Create Metadata for Contacts".
Let me know if it helps!
These metadata are created, so I keep thinking that is covered? Than, in my logic, I should fill them with the right contact_id and the input from my form et voila. But I still get my 404-errorcode when I make the call to $mj->contactdata...
In method updateContactData
, the ID
field you're using references the ID of a ContactData
ressource. You have to change it to ContactID
to reference your contact here. You can find this field in our reference: http://dev.mailjet.com/email-api/v3/contactdata/
I tried contactID, but still get the same result. This is my code now: function updateContactData($id, $data) { $mj = new Mailjet(); $params = array( 'contactID' => $id, 'Data' => $data, 'method' => 'PUT' );
$result = $mj->contactdata($params);
if ($mj->_response_code == 200)
echo "success - data changed";
else
echo "error - ".$mj->_response_code;
return $result;
}
function getContactData($id) { $mj = new Mailjet(); $params = array( 'contactID' => $id, 'method' => 'GET' );
$result = $mj->contact($params);
echo "Opgevraagde contactid: ".$id;
var_dump($result);
if ($mj->_response_code == 200)
echo "success - data gevonden";
else
echo "error - ".$mj->_response_code;
return $result;
} function getContact($list_id, $email) { $mj = new Mailjet(); $params = array( "method" => "GET", "ContactsList" => $list_id, "ContactEmail" => $email, ); $mj->listrecipient($params); return $mj->_response->Data[0]->ID; } and this is my result. My ID is not found, allthough my function getContact() an ID returns, auto-incremented it seems, every time I add a new contact.
Can you please try with ContactID
, respecting the case please?
Ow... this seemed embarrassing, but it doesn't solve everything. I get a result now, from my getContactData-function: I get an array with 10 contacts-objects. The emailadresses don't exist in my contactlist. I think I deleted them. When I run it again, with the newest ContactID - incremented - I get the same list of contacts. My function updateContactData still gives the 404-errorcode...(and the ContactID is case-respecting...)
Hello,
I guess the problem comes from your getContact
function.
In this wrapper, when you need to get a unique result, you need to use VIEW
instead of GET
and to provide an ID
or unique
field.
In that case, because you want to retrieve the ID
, you'll need to provide the unique
field, which for a contact is its email
(see http://dev.mailjet.com/email-api/v3/contact/).
This gives the following function:
function getContact($email) {
$mj = new Mailjet('', '');
$params = array(
"method" => "VIEW",
"unique" => $email
);
$mj->contact($params);
return $mj->_response->Data[0]->ID;
}
Please let me know if it works once you've integrated it in your workflow.
Thanks so much for your patience to keep looking with me. Wishing you all the best for 2015!
My testroutine has become this: I have register the same emailadres over and over again. Every time it is registered, I delete it from the list.
With this new getContact-function - with the email-adress as unique identifier - I get the same contact-ID over and over again. So I conclude deleting is nog deleting, but keeping a contact in some sort of trash, keeping its ID save. With the old getContact-function - with the list-ID, the Email-adress and the GET - I get an incrementing ID, a new one for every time I register the email-adress. But the ID I àm getting - with the new function ànd with the old function - still gives me the 10 deleted contacts from the getContactData-function and the 404-error in the updateContactData.
I'm thinking: could I use the email as identifier for the updateContactData too? I tried it, but making this $params-array still gives the 404:
$params = array(
'Email' => $email,
'Data' => $data,
'method' => 'PUT'
);
Hey,
Sorry for the late answer. Thanks, all the best for 2015 too :)
Yes you can use the email as identifier for the updateContactData too, this way:
function updateContactData($email) {
$mj = new Mailjet('', '');
$data = array(array('Name' => 'lastname', 'Value' => 'Jet'), array('Name' => 'firstname', 'Value' => 'Mail'));
$params = array(
'unique' => $email,
'Data' => $data,
'method' => 'PUT'
);
$result = $mj->contactdata($params);
if ($mj->_response_code == 200)
echo "success - data changed";
else
echo "error - ".$mj->_response_code;
return $result;
}
Just replace the data
array with your data and call this function with an email as a string.
Please let me know if it's fine from your side.
YES!!!! Finally! Thank you so much! The "unique"-keyword did the trick! Thanks again!
Thanks you a lot for this tip ! With the unique key instead of ContactID it works perfectly.
We just released a new PHP API wrapper. It is now more PHP-ish (including namespace, PSR-0 compliance, and globally a better architecture). All it requires is PHP 5.4
In addition to our API Guides, we would be happy to help you get started with it.
Guillaume, from Mailjet!
I found your mailjet-apiv3-php-simple-library and it seems to be exactly what I'm looking for, because I want to extend the formdata, in the widget of the original plugin for Wordpress: I want to register nog only the emailaddress, but also a name or the name of an organisation.
I thought the function updateContactData() would help me with that. I managed to register the contact bij using the addContact-method of the original plugin for Wordpress, I have captured the contact_id and I use this contact_id as a parameter for updateContactData().
The response-code I get is a 404. I think that means that I'm doing a call to a non-existing method. What could be wrong?