【问题标题】:How to decode Json with Curl? [duplicate]如何用 Curl 解码 Json? [复制]
【发布时间】:2018-03-19 23:23:33
【问题描述】:

我想从 json 中回显“Active”这个词。

“状态”:“活动”

PHP/CURL:

$ch = curl_init("http://ip:port/player_api.php?username=$username&password=$password");
    $headers = array();
    $headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0";

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $result = curl_exec($ch);
// Now i need to decode the json

json:

{"user_info":{"username":"test","password":"test","message":"","auth":1,"status":"Active","exp_date":null,"is_trial":"0","active_cons":"0","created_at":"000","max_connections":"1","allowed_output_formats":["m3u8","ts","rtmp"]},"server_info":{"url":"111.111.111","port":"80","rtmp_port":"80","timezone":"test","time_now":"2018-03-20"}}

【问题讨论】:

  • @ObsidianAge 不,我知道如何用 php 解码。但我需要知道如何用 curl 解码
  • 您将 JSON 存储为 PHP 变量 $result;您需要使用 PHP 对其进行解码。为什么你认为你需要用 CURL 本身来解码它? CURL 不知道返回数据的结构。您绝对需要使用 PHP 解码您的 JSON,并且该链接会告诉您确切的操作方法。

标签: php curl


【解决方案1】:

正如黑曜石时代所说,您不能使用 CURL 来解码 JSON。你可以使用 PHP:

你可以使用 CURL 来获取 JSON 文件(我想你已经有了):

$url = 'http://ip:port/player_api.php?username=$username&password=$password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
curl_close($ch);

然后你可以使用PHP解码JSON得到status = active。

$data = json_decode($json, true);

echo $data['user_info']['status'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多