file_get_contents未捕获所需数据

问题描述 投票:0回答:1
$data = file_get_contents($url);

$result = json_decode($data, true); 

使用上面的代码,我想将url的以下输出捕获到$ data,但是file_get_contents没有为$ data提供任何内容。请对此提供帮助,也请让我知道PHP中还有其他方法可以做到这一点。

{
   "destination_addresses" : [ "Boyces Rd, Wisbech PE13 2JT, UK" ],
   "origin_addresses" : [ "Battersea, London SW8 3QR, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "174 km",
                  "value" : 173989
               },
               "duration" : {
                  "text" : "2 hours 42 mins",
                  "value" : 9702
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
php file-get-contents
1个回答
0
投票

您可以做很多事情

例如:

$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可以是另一个解决方案:

$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);
© www.soinside.com 2019 - 2024. All rights reserved.