0

I need to be able to add new XML to a page I access via an API.

I currently access the page via a link something like this:

https://myUsername:myPassword@api.dotmailer.com/v2/address-books/3236359/contacts

The URL is generated by using a few auth variables where I have data stored:

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

This gives me an XML list with data like this:

<ArrayOfApiContact xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apiconnector.com">
  <ApiContact>
    <Id>1058905201</Id>
    <Email>email@email.co.uk</Email>
    <OptInType>Unknown</OptInType>
    <EmailType>Html</EmailType>
    <DataFields i:nil="true"/>
    <Status>Subscribed</Status>
  </ApiContact>
</ArrayOfApiContact>

I need to add a new <apicontact>.

I need to create a new random ID that does not already exist in the XML.

I need to add the email address which I have stored in $useremail

The other bits (opt in, email type, data fields, status) can all be hardcoded standard as seen above.

How would you do this using PHP?

This needs to use REST as I cannot support SOAP on my server.

This documentation specifies a function called PostAddressBookContacts()

As requested I have already tried using cURL, here is what I have tried:

$useremail = array("Email" => "email@email.com");                                                                    
$data_string = json_encode($useremail); 
$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);

Also tried:

$auth_url = 'https://api.dotmailer.com/v2/address-books/{addressBookId}/contacts';

$curl = curl_init($auth_url);
$curl_post_data = array(
        'username' => $apipassword,
        'password' => $apipassword,
        'contact' => $contact,
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Francesca
  • 26,842
  • 28
  • 90
  • 153
  • @JayBlanchard that question makes no mention of adding new entries via php. It discusses parsing XML. – Francesca Sep 11 '14 at 15:12
  • Mh, password into url!? Why don't you use HTTP/1.0 Base Auth? – DonCallisto Sep 11 '14 at 15:14
  • Please share what you have tried. – Jay Blanchard Sep 11 '14 at 15:14
  • @JayBlanchard Many things. I have attempted use of cURL and had zero luck. I will add every cURL variation I have tried. – Francesca Sep 11 '14 at 15:15
  • And yet you ask, "How would you do this using PHP?" @Francesca Not only there, but your title as well. – Jay Blanchard Sep 11 '14 at 15:15
  • So you're saying that you would do this via cURL? Sorry I don't get your point. I cannot get this to work via cURL. I was looking for other options. – Francesca Sep 11 '14 at 15:17
  • @JayBlanchard Can you please explain where in the "possible duplicate" question you provided it explains how to add additional entries? I cannot find it. – Francesca Sep 11 '14 at 15:20
  • There are many entries in the linked question (http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) that will allow you to manipulate XML files. Is this a file on a remote server that you're trying to manipulate? Does the API owner expose a method for editing an XML file? – Jay Blanchard Sep 11 '14 at 15:21
  • Yes there is a method as mentioned in my OP. PostAddressBookContacts(). Yes this is on a remote server. – Francesca Sep 11 '14 at 15:22
  • What errors did you get when you used cURL? – Jay Blanchard Sep 11 '14 at 15:28
  • I didn't get any errors, it just didn't do anything. I have errors switched on. It posted me back to the same page (myscript.php) however I am not happy with the two examples provided above as they do not mention the method PostAddressBookContacts() but I cannot find any cURL documentation that discusses using methods in requests. – Francesca Sep 11 '14 at 15:31

0 Answers0