Laravel中file_get_contents的替代方案是什么?

时间:2019-01-01 06:00:30

标签: php laravel

file_get_contents($url) 有效,但Laravel功能不起作用。

此URL上的文件可用

$url = "https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat";

这个简单的PHP函数正在工作并检索文件

$content = file_get_contents($url);

但是下面提到的所有这些Laravel函数都显示此错误:

  

“文件在路径中不存在   https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat

下面提供了代码:

use File; // i have included this before class
use Storage; // i have included this before class

    $url = "https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat";
    $content = File::get($url); // not working

    $content = Storage::get($url); // not working

    $content = File::get(url($url)); // not working

    $content = file_get_contents($url); // working fine

    return $content;

2 个答案:

答案 0 :(得分:1)

我建议使用guzzlehttp(Github Page

易用性:

安装:

 composer require guzzlehttp/guzzle

发送请求并获得响应:

$client = new \GuzzleHttp\Client();
$res = $client->get($url);
$content = (string) $res->getBody();

答案 1 :(得分:0)

或者,我们可以使用文件外观。为了从外部站点获取信息,我们可以使用guzzle。 链接为https://github.com/guzzle/guzzle