簡體   English   中英

使用cURL和PHP進行POST請求

[英]using cURL with PHP for POST request

抱歉,如果這真的很模糊,我是cURL和與API交互的新手。

目前,我已經創建了一個帶有所需身份驗證詳細信息的URL,該URL將訪問API(dotmailer)並顯示具有給定ID的通訊錄中的聯系人。

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

這使我有點像這樣:

https://開頭的用戶名:password@api.dotmailer.com/v2/address-books/listID/contacts

這是該頁面的XML

<ApiContact>
<Id>1058905201</Id>
<Email>email@email.com</Email>
<OptInType>Unknown</OptInType>
<EmailType>Html</EmailType>
<DataFields i:nil="true"/>
<Status>Subscribed</Status>
</ApiContact>
</ArrayOfApiContact>

現在,我需要將變量發布到API。 我的變量是$useremail

我打算使用cURL來做到這一點-我正在使用PHP,並避免使用SOAP來代替REST。

我對此完全陌生,但是我嘗試了一些不起作用的代碼,但是嘿,我嘗試了!

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

我知道這還行,但:

  • 這會將我從我的API發送到404頁面
  • 我知道在某個地方我錯過了將其添加到XML上的<email>的操作
  • 該文檔引用了我沒有提到的PostAddressBookContacts()方法,所以它去哪里了?

抱歉,如果不清楚。 我感謝任何人的建議。

首先,如果您在瀏覽器中啟動此URL:

https://username:password@api.dotmailer.com/v2/address-books/listID/contacts

對你起作用嗎?

如果是這樣,則回顯$ auth_url。

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

並檢查您的網址是否正確構建。 然后:

編輯

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM