php请求json并打印其中某个值的方法

php建设网站时,有时候需要远程请求json数据,但是json数据中我们只需要其中某一个值应该怎么办呢?今天就来说说用php请求json并打印其中某个值的方法。

举例我这里需要请求百度语音合成的tok值,请看下面的代码:

<?php 
$url='https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=APPID值&client_secret=APPsecret值&'; 
$html=file_get_contents($url); 
echo ($html);
?>

php请求百度tok值json结果

通过上面的代码我们请求出来的结果,有很多个值,但是我只想要refresh_token一个值怎么办?请看下面的代码:

<?php 
$url='https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=APPID值&client_secret=APPsecret值&'; 
$html=file_get_contents($url);
$object = json_decode($html);
echo $object->refresh_token;
?>

PHP远程请求百度语音合成json并打印refresh_token值结果

通过上面的代码,我们就可以拿到json结果中第一个refresh_token的值了,效果如上图。

如果内容有帮助,就点个赞吧!

转载注明出处:http://www.12564.cn/show-199.html

如有疑问请联系 QQ:644233191   微信:zw1688k