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");
?>
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"));
?>
Nathan has it. You may want to explore the curl library for a more robust HTTP request approach, too.
精彩评论