简体   繁体   English

JSON错误“试图获取非对象的属性”

[英]JSON Error “Trying to get property of non-object”

I have a problem on my code: 我的代码有问题:

Look my JSON: 看我的JSON:

 { "informations": { "version": "1_0_1_ALPHA", "terms": "https://dev-time.eu/fr/", "update": "20/12/2018", "game": "bo4", "reponse": "success" } , "multiplayer": { "map_code": "mp_urban", "map": "Arcenal", "dlc": "0", "date": "Sortie du jeu" } } 

My code (PHP): 我的代码(PHP):

<?php $maps_name = file_get_contents("https://dev-time.eu/api/api__callofduty?game=bo4&map=mp_urban&type=mp"); ?>
        <?php $parsed_map = json_decode($maps_name); ?>
        <?= var_dump($maps_name); ?>
        "<?= $parsed_map->{'informations'}->{'version'}; ?>"

My var_dump return: 我的var_dump返回:

 string(354) "{ "informations": { "version": "1_0_1_ALPHA", "terms": "https://dev-time.eu/fr/", "update": "20/12/2018", "game": "bo4", "reponse": "success" } , "multiplayer": { "map_code": "mp_jungle2", "map": "Jungle", "dlc": "0", "date": "Sortie du jeu" } }" " 

在此输入图像描述

This is the life savior for you :) 这是你的生命救星 :)

 function remove_utf8_bom($str)
 {
    $bom_char = pack('H*','EFBBBF');
    return preg_replace("/^$bom_char/", '', $str);
  }

Full Working code with CURL , file_get_contents() will also work 使用CURLfile_get_contents() 完整工作代码也可以工作

<?php
function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://dev-time.eu/api/api__callofduty?game=bo4&map=mp_urban&type=mp",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
    ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    $response = remove_utf8_bom($response);   
}
$d = json_decode($response);
echo $d->informations->version;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM