2015-12-03 52 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