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:password@api.dotmailer.com/v2/address-books/listID/contacts

Here is the XML from that page

<ApiContact>
<Id>1058905201</Id>
<Email>email@email.com</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.

user1486133
  • 1,309
  • 3
  • 19
  • 37
  • possible duplicate of [Passing $\_POST values with cURL](http://stackoverflow.com/questions/28395/passing-post-values-with-curl) – Patrick Q Sep 11 '14 at 13:47
  • so you need to post xml using curl or only email var? – Rakesh Sharma Sep 11 '14 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. – user1486133 Sep 11 '14 at 13:50

1 Answers1

1

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

https://username:password@api.dotmailer.com/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@email.com");                                                                    
$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);
Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
  • 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? – user1486133 Sep 11 '14 at 13:55
  • 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 http://php.net/manual/en/class.soapclient.php. – Artur Kedzior Sep 11 '14 at 14:10
  • Yes the API supports REST. I cannot use SOAP. Here is the link to the method I mentioned in the API's REST documentation. https://api.dotmailer.com/v2/help/wadl#PostAddressBookContacts – user1486133 Sep 11 '14 at 14:14
  • I edited my answer to post ApiContact as JSON. https://api.dotmailer.com/v2/help/wadl#ApiContact – Artur Kedzior Sep 11 '14 at 14:30
  • I have tried it again but it does nothing. No errors just nothing. – user1486133 Sep 11 '14 at 15:01
  • Hmmm. Get this plugin and do some tests https://addons.mozilla.org/en-US/firefox/addon/restclient/. Set the url: https://username:password@api.dotmailer.com/v2/address-books/listID/contacts. Set method POST and send this request: {"Email":"example@example.com"}. See what errors it gives you. – Artur Kedzior Sep 11 '14 at 16:12