2015-12-03 51 views
2

我正在使用代碼點火器框架,並希望使用PHP Curl從外部服務器API中提取數據。我之前沒有用過這個,所以有點卡住了,我的研究即將開始。PHP Curl獲取標題

我有這個工作在谷歌郵遞員,但只需要複製這在PHP中。

谷歌郵遞員配置: enter image description here

我捲曲語法:

$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的最佳選擇,那麼請告知。由於

+0

使用curl_error($ CH),看看爲什麼捲曲返回false,false表示錯誤。我敢打賭,這與CURLOPT_VERIFYPEER有關,因爲 – hanshenrik

+0

謝謝@hanshenrik,apprecaite你的時間。我已經更新了我的問題以顯示我目前的狀況,感謝您的幫助。 – Smudger

+0

如果這在郵遞員的作品,準確地告訴我什麼郵遞員發送...鉻開發人員控制檯應該能夠告訴你 – hanshenrik

回答

0

評論USER_AGENT參數或使用該配置

 $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     //curl_setopt($ch, CURLOPT_POST, 1); 
     //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); 
     curl_setopt($ch, CURLOPT_HEADER, 1); 
     //curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($ch, CURLOPT_VERBOSE, 1); 
     //curl_setopt($ch, CURLOPT_PROXY, PROXY); 

     $result = curl_exec($ch); 
     $error = curl_error($ch); 
     if ($error) echo $error; 
+0

謝謝@Aldo,試圖運行你的代碼,但密切配合我的,現在任何想法,爲什麼頭不會正確應用?謝謝, – Smudger