使用cURL与PHP的POST请求
很抱歉,如果这真的很模糊,我是新来的cURL和API的交互。使用cURL与PHP的POST请求
目前我创建了一个URL,其中包含将要访问API(点播程序)并使用给定ID在地址簿中显示联系人所需的身份验证详细信息。
$auth_url = 'https://' . $apiemail .':' . $apipassword . '@api.dotmailer.com/v2/address-books/' . $listID .'/contacts';
哪个地方送我有点像这样:
https://username:[email protected]/v2/address-books/listID/contacts
下面是从页
<ApiContact>
<Id>1058905201</Id>
<Email>[email protected]</Email>
<OptInType>Unknown</OptInType>
<EmailType>Html</EmailType>
<DataFields i:nil="true"/>
<Status>Subscribed</Status>
</ApiContact>
</ArrayOfApiContact>
现在我需要一个变量POST到API的XML。我的变量是$useremail
我打算使用cURL来做到这一点我使用PHP并避免使用REST代替SOAP。
我完全陌生,但我有一些代码不起作用,但嘿,我试过了!
// 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);
我知道这是路要走,但:
- 这给我发来一个404页的从我的API
- 我知道的地方我错过了,将在此添加到
<email>
行动XML - 该文档引用了我没有提及的方法
PostAddressBookContacts()
,那么这是何去何从?
对不起,如果这是模糊的。我感谢任何人的建议。
首先,如果你在浏览器中启动此网址:
https://username:[email protected]/v2/address-books/listID/contacts
它为你工作?
如果是这样,请回显$ auth_url。
$auth_url = 'https://' . $apiemail .':' . $apipassword . '@api.dotmailer.com/v2/address-books/' . $listID .'/contacts';
echo $auth_url;
并检查您的网址是否构建正确。然后:
编辑 [传递$ \ _ POST值与卷曲]的
$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);
嗨。是的,链接是正确的,是的,我已经回应,并可以确认它是正确的网址。我不明白的是如何使用方法PostAddressBookContacts()将变量$ useremail发送到此。你还没有提到那部分? – user1486133 2014-09-11 13:55:20
我的回答给你一个REST的例子,但你必须确保API支持它。如果它只提到PostAddressBookContacts(),最好的办法是使用SOAPClient http://php.net/manual/en/class.soapclient.php。 – 2014-09-11 14:10:53
是的API支持REST。我不能使用SOAP。这里是我在API的REST文档中提到的方法的链接。 https://api.dotmailer.com/v2/help/wadl#PostAddressBookContacts – user1486133 2014-09-11 14:14:55
可能重复(http://stackoverflow.com/questions/28395/passing-post-values-with-curl) – 2014-09-11 13:47:15
所以你需要使用curl发布xml或者只发邮件var? – 2014-09-11 13:48:08
@RakeshSharma我真的不确定,因为API文档没有解释,我不知道我会如何发现。我正在尝试将新联系人添加到列表中。 PostAddressBookContacts()方法应该这样做,但我不知道应该在哪里引用它。 – user1486133 2014-09-11 13:50:37