PHP Curl获取标题

时间:2015-12-03 10:40:14

标签: php codeigniter api curl get

我正在使用代码点火器框架,并希望使用PHP Curl从外部服务器api中提取数据。我以前没用过这个,所以有点卡住,我的研究空洞。

我在谷歌邮递员工作,但只需要在PHP中复制它。

谷歌邮递员配置: enter image description here

My Curl语法:

$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的最佳选项,那么请提供建议。感谢

1 个答案:

答案 0 :(得分: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;