且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从URL获取JSON对象

更新时间:2023-02-23 20:32:19

$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;

为此,file_get_contents要求启用allow_url_fopen.可以在运行时完成以下操作:

For this to work, file_get_contents requires that allow_url_fopen is enabled. This can be done at runtime by including:

ini_set("allow_url_fopen", 1);

您还可以使用 curl 来获取网址.要使用curl,您可以使用在此处找到的示例:

You can also use curl to get the url. To use curl, you can use the example found here:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'url_here');
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);
echo $obj->access_token;