gpt4 book ai didi

php - JSON 错误 "Trying to get property of non-object"

转载 作者:搜寻专家 更新时间:2023-10-31 20:57:37 29 4
gpt4 key购买 nike

我的代码有问题:

看看我的 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"	} }

我的代码(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'}; ?>"

我的 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"	} }" "

enter image description here

最佳答案

这是你的救星 :)

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

完整的工作代码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;
?>

关于php - JSON 错误 "Trying to get property of non-object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53887258/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com