我使用API来显示访问者的信息,并在注册到网站时将一些细节存储在数据库中。我从API获得的响应是这样的:
{"data":{"ua_type":"Desktop","os_name":"Windows","os_version":"10","browser_name":"Firefox","browser_version":"52.0","engine_name":"Gecko","engine_version":"20100101"}}
我如何去做,以便我可以将一些值放入变量中,例如
$ device = ua_type;
$ os = os_name;
等...
任何帮助都会很棒。
我已经看过json_decode,就像你提到的一些,下面是我的一些代码,由于某种原因它不会显示任何值,我的代码下面有什么问题? print_r($ result);工作正常,但将结果放在变量似乎没有输出任何东西。提前感谢您的帮助!
$result = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($result);
$apiresult = json_decode($result);
$ua_type=$apiresult->{'ua_type'};
$os_name=$apiresult->{'os_name'};
$os_version=$apiresult->{'os_version'};
$browser_name=$apiresult->{'browser_name'};
$browser_version=$apiresult->{'browser_version'};
$ua_brand=$apiresult->{'ua_brand'};
$ua_name=$apiresult->{'ua_name'};
echo "<br>";
echo "Device" . $ua_type;
echo "<br>";
echo "OS" . $os_name;
echo "<br>";
echo "OS Version" . $os_version;
echo "<br>";
echo "Browser" . $browser_name;
echo "<br>";
echo "Browser Version" . $browser_version;
echo "<br>";
echo "Mobile Make" . $ua_brand;
echo "<br>";
echo "Mobile Model" . $ua_name;
答案 0 :(得分:0)
正如您对帖子的一些评论所建议的那样,您应该阅读json_decode,因为它会完全符合您的要求:)
$data = json_decode('{"data":{"ua_type":"Desktop","os_name":"Windows","os_version":"10","browser_name":"Firefox","browser_version":"52.0","engine_name":"Gecko","engine_version":"20100101"}}', true);
将导致以下数据结构:
[
"data" => [
"ua_type" => "Desktop",
"os_name" => "Windows",
"os_version" => "10",
"browser_name" => "Firefox",
"browser_version" => "52.0",
"engine_name" => "Gecko",
"engine_version" => "20100101",
],
]
然后,您可以简单地将变量分配给您需要的任何位,例如:
$ua_type = $data['data']['ua_type']