如何从PHP调用和解码JSON Web服务?

时间:2021-12-04 08:35:34

From within PHP, how can I call an external JSON web service and then decode the returned string?

从PHP中,如何调用外部JSON Web服务然后解码返回的字符串?

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

?>

2 个解决方案

#1


19  

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

#2


0  

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

内森有它。您可能还想探索curl库以获得更强大的HTTP请求方法。

#1


19  

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

#2


0  

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

内森有它。您可能还想探索curl库以获得更强大的HTTP请求方法。