且构网

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

从URL获取文件内容?

更新时间:2022-05-07 22:31:16

根据您的PHP配置,该 可能很容易使用:

Depending on your PHP configuration, this may be a easy as using:

$jsonData = json_decode(file_get_contents('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json'));

但是,如果系统上未启用allow_url_fopen,则可以通过CURL读取数据,如下所示:

However, if allow_url_fopen isn't enabled on your system, you could read the data via CURL as follows:

<?php
    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json');
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

    $jsonData = json_decode(curl_exec($curlSession));
    curl_close($curlSession);
?>

顺便说一句,如果只需要原始JSON数据,则只需删除json_decode.

Incidentally, if you just want the raw JSON data, then simply remove the json_decode.