TeddyR
TeddyR

Reputation: 1213

How do I call and decode a JSON web service from 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");

?>

Upvotes: 6

Views: 17801

Answers (2)

nathan
nathan

Reputation: 5452

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

Upvotes: 19

Funkatron
Funkatron

Reputation: 892

Nathan has it. You may want to explore the curl library for a more robust HTTP request approach, too.

Upvotes: 0

Related Questions