如何解码卷曲输出?

时间:2016-03-14 10:13:50

标签: php curl

请帮我解码下面的输出,这是我运行cURL时得到的。

这是我的代码:

$headers = array(
            'Accept:application/json',
            'X-TB-PARTNER-AUTH: 45504852:abcdksalkrjwejkrjr'
        );


        $process = curl_init();
        curl_setopt($process, CURLOPT_URL,"https://api.opentok.com/session/create");
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($process, CURLOPT_HEADER, 1);
        curl_setopt($process, CURLOPT_POST, 1);
        curl_setopt($process, CURLOPT_POSTFIELDS, 'p2p.preference=disabled');
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
        $server_output = curl_exec($process);
        print_r($server_output);

我的输出是:

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 14 Mar 2016 10:10:19 GMT
Content-Type: application/json
Connection: keep-alive
Access-Control-Allow-Origin: *
Content-Length: 180
Strict-Transport-Security: max-age=31536000; includeSubdomains

[{"session_id":"2_MX40NTUwNDg1Mn5-MTQ1Nzk1MDIxOTVNGZmUwS1N3bFF2LzhvREZORVN-fg","partner_id":"404852","create_dt":"Mon Mar 12 03:10:19 PDT 2016","media_server_url":""}]

2 个答案:

答案 0 :(得分:1)

您是否对输出中包含HTTP标头这一事实感到困惑?

如果是这样 - 放弃这一行:curl_setopt($process, CURLOPT_HEADER, 1);

负责在输出中包含HTTP标头。 (见http://php.net/manual/en/function.curl-setopt.php

答案 1 :(得分:1)

使用json_decode函数将其解析为对象或数组:

$result = '[{"session_id":"2_MX40NTUwNDg1Mn5-MTQ1Nzk1MDIxOTVNGZmUwS1N3bFF2LzhvREZORVN-fg","partner_id":"404852","create_dt":"Mon Mar 12 03:10:19 PDT 2016","media_server_url":""}]';
echo "<pre>";
$json = json_decode($result,true);
print_r($json);// You may access print_r($json{0}->create_dt); etc

参考文献: