Php 如何解码JSON以获取变量的值

Php 如何解码JSON以获取变量的值,php,json,Php,Json,JSON URL: 我正在尝试解码这个json,但出现了错误。我做错了什么

JSON URL:
我正在尝试解码这个json,但出现了错误。我做错了什么<任何帮助都将不胜感激

$json = file_get_contents("https://www.mylivepolls.com/api.php");
$data = json_decode($json);
$var = $data->mchdata->match[0]->id;
echo $var;

试试这个,或者你可以使用
curl
而不是
file\u-get\u-contents
使用cookie-jar

$json = file_get_contents("https://www.mylivepolls.com/api.php");
$data = json_decode($json, true);

if(is_array($data) && isset($data['mchdata']) && isset($data['mchdata']['match'])) {
    $matches = $data['mchdata']['match'];

    if(is_array($matches)) {
        foreach($matches as $match) {
            $id = $match['id'];
        }
    }
    $current_match = current($matches);
    echo $var = $current_match['id'];
}
使用Curl:

$ckfile = tempnam (sys_get_temp_dir(), 'apicookiename');

$handle = curl_init("https://www.mylivepolls.com/api.php");

curl_setopt ($handle, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($handle, CURLOPT_COOKIEFILE, $ckfile);

curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);

$json = curl_exec($handle); 
$data = json_decode($json, true);

if(is_array($data) && isset($data['mchdata']) && isset($data['mchdata']['match'])) {
    $matches = $data['mchdata']['match'];

    if(is_array($matches)) {
        foreach($matches as $match) {
            $id = $match['id'];
        }
    }
    $current_match = current($matches);
    echo $var = $current_match['id'];
}

谢谢

您的错误是什么?@pascalzoet这是错误:注意:尝试在中获取非对象的属性。这对我有效。你的文件是否有效?它对我也有效。您的服务器可能不允许file\u get\u contents()。从
file\u get\u contents
调用中转储
$json