Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call and decode a JSON web service from PHP?

Tags:

json

php

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");

?>
like image 863
TeddyR Avatar asked Jul 07 '10 22:07

TeddyR


People also ask

How can access JSON decoded data in PHP?

PHP - Accessing the Decoded Values$obj = json_decode($jsonobj);

Which PHP function decodes JSON structures?

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.

What is JSON decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

How do I decode a JSON file?

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.


1 Answers

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"));
?>
like image 99
nathan Avatar answered Oct 19 '22 23:10

nathan