【问题标题】:PHP Curl Get with headersPHP Curl 获取标题
【发布时间】:2015-12-03 10:40:13
【问题描述】:

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

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

谷歌邮递员配置:

我的 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 的最佳选择,那么也请提出建议。谢谢

【问题讨论】:

  • 使用 curl_error($ch) 查看 curl 返回 false 的原因,false 表示错误。我敢打赌这与 CURLOPT_VERIFYPEER 思想有关
  • 谢谢@hanshenrik,感谢您的时间。我已经更新了我的问题以显示我目前的情况,感谢您的帮助。
  • 如果这在邮递员中有效,请告诉我邮递员发送的确切内容...... Chrome 开发者控制台应该能够告诉你

标签: php codeigniter api curl get


【解决方案1】:

注释 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;

【讨论】:

  • 感谢@Aldo,尝试运行您的代码,但现在与我的代码非常匹配,关于为什么标题不能正确应用的任何想法?谢谢,
猜你喜欢
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多