我正在使用代碼點火器框架,並希望使用PHP Curl從外部服務器API中提取數據。我之前沒有用過這個,所以有點卡住了,我的研究即將開始。PHP Curl獲取標題
我有這個工作在谷歌郵遞員,但只需要複製這在PHP中。
我捲曲語法:
$url = "https://api.doamin.com/v1/config/limits";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Connection: Keep-Alive',
'account:A004',
'key : 1234-12'
));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$request= curl_getinfo($ch);
var_dump($request);
echo "<br><br><br>Reply:";
$content = curl_exec($ch);
我有這方面的工作在一定程度上,我現在從我的服務器獲得響應但是我在發送標題我語法沒有被應用。頁面輸出如下:
array(22) { ["url"]=> string(94) "https://api.domain.com/v1/config/limits" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }
Reply:HTTP/1.1 401 Unauthorized Server: nginx Date: Thu, 03 Dec 2015 14:46:26 GMT Content-Type: application/json; charset=utf-8 Content-Length: 51 Connection: keep-alive Access-Control-Allow-Origin: * {"status":"failure ","data":"Authentication Error"}
爲什麼不顯示我的頭,他們都喜歡的內容類型的地方是null
而非application/json
在代碼中被設置。
非常感謝您的幫助。
如果curl不是用來從PHP訪問此API的最佳選擇,那麼請告知。由於
使用curl_error($ CH),看看爲什麼捲曲返回false,false表示錯誤。我敢打賭,這與CURLOPT_VERIFYPEER有關,因爲 – hanshenrik
謝謝@hanshenrik,apprecaite你的時間。我已經更新了我的問題以顯示我目前的狀況,感謝您的幫助。 – Smudger
如果這在郵遞員的作品,準確地告訴我什麼郵遞員發送...鉻開發人員控制檯應該能夠告訴你 – hanshenrik