【问题标题】:Curl Php Returns NULL in browser onCurl Php 在浏览器中返回 NULL
【发布时间】:2015-12-08 02:27:09
【问题描述】:

我正在尝试对第三方票务系统进行身份验证,连接并检索代理信息。然后我想解析检索到的文件,以便仅显示特定信息,例如“id”和“active_since”,但在浏览器中出现 NULL 错误。有什么帮助吗?

第三方返回的文件

[agent] => Array
        (
            [active_since] => 2015-11-30T08:09:26-01:00
            [available] => 1
            [created_at] => 2015-05-14T19:15:18+00:00
            [id] => XXXXX 
            [occasional] => 
            [points] => 5520
            [scoreboard_level_id] => 5000402007
            [signature] => 
            [signature_html] => 

下面是 PHP CURL 代码。

 <?php
    $username = "xxxxxxx";
    $password = "xxxxxxx";
    $url = 'http://support.xxxx.com/agents/xxx132067';

    $cURL = curl_init();
    curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($cURL, CURLOPT_URL, $url);
    curl_setopt($cURL, CURLOPT_HTTPGET, true);
    curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Accept: application/json',
        'header' => "Authorization: Basic " . base64_encode("$username:$password")      

    )
    );

    $result = file_get_contents($url);
    // Will dump a beauty json :3
    var_dump(json_decode($result, true));

    $array["active_since"];

    ?>

【问题讨论】:

    标签: php arrays json xml curl


    【解决方案1】:

    Authorization 标头中删除'header' =&gt;。它应该如下:

    "Authorization: Basic " . base64_encode("$username:$password")
    

    而且我根本没有看到你在调用 curl。用正确的 curl 调用替换您的 file_get_contents

    $result = curl_exec($cURL);
    curl_close($cURL);
    

    【讨论】:

    • 我没有收到 NULL 错误,但我仍然无法仅获取活动部分...只需获取 json 格式的整个页面。
    猜你喜欢
    • 2015-05-15
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多