我需要能够将新XML添加到我通过API访问的页面。
我目前通过以下链接访问该页面:
https://myUsername:myPassword@api.dotmailer.com/v2/address-books/3236359/contacts
使用一些我存储数据的auth变量生成URL:
$auth_url = 'https://' . $apiemail .':' . $apipassword . '@api.dotmailer.com/v2/address-books/' . $listID .'/contacts';
这给了我一个包含如下数据的XML列表:
<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>
我需要添加一个新的<apicontact>
。
我需要创建一个XML中尚不存在的新随机ID。
我需要添加我在$useremail
其他位(选择输入,电子邮件类型,数据字段,状态)都可以是硬编码标准,如上所示。
你会如何使用PHP做到这一点?
这需要使用REST,因为我无法在我的服务器上支持SOAP。
此documentation指定名为PostAddressBookContacts()
根据要求,我已经尝试过使用cURL,这是我尝试过的:
$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);
也尝试过:
$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);