請教一個PHP CURL的POST提交遇到的問題
這兩天在學習搜狐的sendCloud平台,遇到一個問題。
有一個介面:
https://sendcloud.sohu.com/webapi/list.create.json
官方給的文檔是這樣的:
* 請求介面(建議使用post請求,注意不要使用multipart-post):
/list.create
* 參數說明:
參數 類型 是否必須 說明 描述
api_user string 是 請求SC認證帳號
api_key string 是 請求 SC認證密碼
addres sstring 是 列表別稱地址 例如:[email protected] Developers
name string 是 列表名稱 不能大於16個中文字
description string 否 列表描述 不能大於85個中文字元
於是,我在命令列輸入這樣:
curl -d "api_user=*&api_key=*&[email protected]&name=demo&description=this is a demo list" https://sendcloud.sohu.com/webapi/list.create.json
提交正確,建立了一個郵件清單。
嘗試構造表單提交,同樣提交正確
但是,用PHP CURL提交,就出了問題,代碼如下:
header('Content-Type:text/html;charset=utf-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_URL, 'https://sendcloud.sohu.com/webapi/list.create.json');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = array(
'api_user' => '*',
'api_key' => '*',
'address' => [email protected]',
'name' => 'Abao建立的測試郵件清單',
'description' => '這是Abao建立的測試郵件清單',
);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
if(false === $result=curl_exec($ch)) {
echo 'false:
';
}
var_dump(json_decode($result,true));
curl_close($ch);
這時候總是返回 bad username/password,如果直接在瀏覽器開啟介面的url也是現實bad username/password。
所以我猜測是資料沒有提交上去。
百度了說設定curl_setopt($ch,CURLOPT_VERIFYHOST,true),但我一設定就報錯
Notice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in F:\www\sendCloud\demo.php on line 13
更加奇怪的是用同樣的代碼,調用/mail.send.json也就是發送郵件的介面,卻能夠正常提交。
請各位幫忙看看,十分感謝!
------解決思路----------------------
輸出curl_error()和curl_errno看看?
------解決思路----------------------
你的表單得到
{"message":"error","errors":["Bad username / password!"]}
你的 php 代碼得到
array(2) { ["message"]=> string(5) "error" ["errors"]=> array(1) { [0]=> string(24) "Bad username / password!" } }
你沒有給出正確的使用者名稱和口令,自然不能得到正確的結果