0

Sorry if this is really vague, I'm new to both cURL and interacting with API's.

Currently I have created a URL with the authentication details required that will access the API (dotmailer) and display the contacts in the address book with the given ID.

$auth_url = 'https://' . $apiemail .':' . $apipassword . '@api.dotmailer.com/v2/address-books/' . $listID .'/contacts';

Which sends me somewhere a little like this:

https://username:[email protected]/v2/address-books/listID/contacts

Here is the XML from that page

<ApiContact>
<Id>1058905201</Id>
<Email>[email protected]</Email>
<OptInType>Unknown</OptInType>
<EmailType>Html</EmailType>
<DataFields i:nil="true"/>
<Status>Subscribed</Status>
</ApiContact>
</ArrayOfApiContact>

Now I need to POST a variable to the API. My variable is $useremail

I was planning to use cURL to do this a I'm using PHP and avoiding SOAP instead using REST.

I'm completely new to this but I had a go with some code that doesn't work, but hey I tried!

// Authenticate
$auth_url = 'https://' . $apiemail .':' . $apipassword . '@api.dotmailer.com/v2/address-books/' . $listID .'/contacts';

// open connection
$ch = curl_init();

// set the URL
curl_setopt($ch,CURLOPT_URL, $auth_url);
curl_setopt($ch,CURLOPT_POST, $useremail);

// execute post
$result = curl_exec($ch);

// close connection
curl_close($ch);

I know this is way off but:

  • This sends me to a 404 page from my API
  • I know somewhere I'm missing the action that would add this to <email> on the XML
  • The documentation makes reference to the method PostAddressBookContacts() which I haven't mentioned, so where does this go?

Sorry if this is vague. I appreciate anyone's advice.

3
  • possible duplicate of Passing $_POST values with cURL Commented Sep 11, 2014 at 13:47
  • so you need to post xml using curl or only email var? Commented Sep 11, 2014 at 13:48
  • @RakeshSharma I'm really not sure at the moment as the API documentation does not explain and I don't know how I would find out. I am trying to add a new contact to the list. The method PostAddressBookContacts() should do this I think but I don't know where I am supposed to reference it. Commented Sep 11, 2014 at 13:50

1 Answer 1

1

First of all, if you launch this url in the browser:

https://username:[email protected]/v2/address-books/listID/contacts

does it work for you?

If so, echo the $auth_url.

$auth_url = 'https://' . $apiemail .':' . $apipassword . '@api.dotmailer.com/v2/address-books/' . $listID .'/contacts';
echo $auth_url;

and check if your url is constructed correctly. Then:

EDIT

$data = array("Email" => "[email protected]");                                                                    
$data_string = json_encode($data); 
$req = curl_init($auth_url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($req, CURLOPT_POST, 1);
curl_setopt($req, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($req, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data_string))                                                                       
); 
$res = curl_exec($req);
curl_close($req);
Sign up to request clarification or add additional context in comments.

6 Comments

Hi. Yes the link is correct, and yes I have echoed and can confirm it is the correct URL. What I do not understand is how to POST the variable $useremail to this using the method PostAddressBookContacts(). You haven't mentioned that part?
My answers gives you an example for REST, but you have to make sure that the API supports it. If it only mentions PostAddressBookContacts() your best bet is to use SOAPClient php.net/manual/en/class.soapclient.php.
Yes the API supports REST. I cannot use SOAP. Here is the link to the method I mentioned in the API's REST documentation. api.dotmailer.com/v2/help/wadl#PostAddressBookContacts
I edited my answer to post ApiContact as JSON. api.dotmailer.com/v2/help/wadl#ApiContact
I have tried it again but it does nothing. No errors just nothing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.