關鍵詞: php語言 curl函數 get post方法 http1.1協議
不用說太多,簡單概括如下:
調用京東接口時,出現$response = null;
但是 生成的帶有sign簽名的 url地址可以在瀏覽器中獲得接口返回的信息
爬了將近一天的坑,都沒有走出來!!最終受高人指點,得到解決.
自己嘗試過得解決方法:
1.別人封裝的curl方法 無效
2.輸出 curl_error 和 curl_errno 無效
3.輸出 curl_getinfo() 無效(查無異樣)
4.各種邏輯檢查
5.sign簽名 測試
最終結果是:
//http1.1協議中的 url 有編碼格式 不可以使用空格,使用+ 替換
**
將 $url 進行如下處理處理
$url = str_replace(’ ', ‘+’, $url);
//http1.1協議中的 url 有編碼格式 不可以使用空格,使用+ 替換
//將 $url 進行如下處理處理
$url = str_replace(' ', '+', $url);
//完整代碼
get請求
public static function getHttps($url)
{
$urls = str_replace(' ', '+', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urls);//設置鏈接
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//設置SSL驗證
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置是否返回信息
$data = curl_exec($ch);//接收返回信息
curl_close($ch);
return $data;
}
post 請求
public static function postHttps($postArr, $url)
{
$urls = str_replace(' ', '+', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//設置SSL驗證
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否檢測服務器的域名與證書上的是否一致
curl_setopt($ch, CURLOPT_URL, $urls);//設置鏈接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置是否返回信息
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($postArr))
);
$res = curl_exec($ch);//接收返回信息
curl_close($ch);
return $res;
}
別人封裝的curl
public static function curl($url, $params = false, $ispost = 0, $https = 0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 對認證證書來源的檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書中檢查SSL加密算法是否存在
}
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);// 此處就是參數的列表,給你加了個?
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
封裝的沒有進行處理,可以自行腦補~~~~
完美解決!!
暫時還沒有進一步深究,寫此文以記錄成長曆程,希望能夠幫助到需要幫助的人!