From within PHP, how can I call an external JSON web service and then decode the returned string?
For example (pseudo-code):
<?php
$var jsonStr = json_decode("http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
?>
PHP - Accessing the Decoded Values$obj = json_decode($jsonobj);
Parsing JSON with PHP PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.
The json_decode() function is used to decode or convert a JSON object to a PHP object.
You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.
you almost had it!
<?php
$jsonObject = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"));
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With