I'm using an API to display information from my visitors and going to store some of the details in a database from when the signup to the site. The response i get from the API is like this:
{"data":{"ua_type":"Desktop","os_name":"Windows","os_version":"10","browser_name":"Firefox","browser_version":"52.0","engine_name":"Gecko","engine_version":"20100101"}}
How do i go about doing it so that i can place some of the values into a variable so for example
$device = ua_type;
$os = os_name;
etc....
Any help would be great please.
I've had a look at json_decode like a few of you have mentioned and below is some of my code, it wont display any of the values for some reason, whats wrong with my code below? The print_r($result); works fine but placing the results in variables doesnt seem to output anything. Thank you in advance for the help!
$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;
As some comments on your post have suggested, you should read up on json_decode as it'll do exactly what you're looking for :)
$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);
Will result in the following datastructure:
[
"data" => [
"ua_type" => "Desktop",
"os_name" => "Windows",
"os_version" => "10",
"browser_name" => "Firefox",
"browser_version" => "52.0",
"engine_name" => "Gecko",
"engine_version" => "20100101",
],
]
Then, you can simply assign variables to whichever bit you need, like so:
$ua_type = $data['data']['ua_type']