2014-09-11 137 views
0

很抱歉,如果這真的很模糊,我是新來的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(),那麼這是何去何從?

對不起,如果這是模糊的。我感謝任何人的建議。

+0

可能重複(http://stackoverflow.com/questions/28395/passing-post-values-with-curl) – 2014-09-11 13:47:15

+0

所以你需要使用curl發佈xml或者只發郵件var? – 2014-09-11 13:48:08

+0

@RakeshSharma我真的不確定,因爲API文檔沒有解釋,我不知道我會如何發現。我正在嘗試將新聯繫人添加到列表中。 PostAddressBookContacts()方法應該這樣做,但我不知道應該在哪裏引用它。 – user1486133 2014-09-11 13:50:37

回答

1

首先,如果你在瀏覽器中啓動此網址:

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

嗨。是的,鏈接是正確的,是的,我已經迴應,並可以確認它是正確的網址。我不明白的是如何使用方法PostAddressBookContacts()將變量$ useremail發送到此。你還沒有提到那部分? – user1486133 2014-09-11 13:55:20

+0

我的回答給你一個REST的例子,但你必須確保API支持它。如果它只提到PostAddressBookContacts(),最好的辦法是使用SOAPClient http://php.net/manual/en/class.soapclient.php。 – 2014-09-11 14:10:53

+0

是的API支持REST。我不能使用SOAP。這裏是我在API的REST文檔中提到的方法的鏈接。 https://api.dotmailer.com/v2/help/wadl#PostAddressBookContacts – user1486133 2014-09-11 14:14:55