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);