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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53993336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:14:01  来源:igfitidea点击:

Whats the alternative for file_get_contents in Laravel?

phplaravel

提问by user3680417

file_get_contents($url)is workingbut Laravel functions not working.

file_get_contents($url)正在工作,但 Laravel 功能不工作。

File is available at this URL

文件在此 URL 上可用

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

this simple PHP function is working and retrieving the file

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

$content = file_get_contents($url);

but all these Laravel functions which mentioned below are showing this error :

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

"File does not exist at path https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat"

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

Code is available below:

代码如下:

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;

回答by Saman Ahmadi

I suggest guzzlehttp (Github Page)

我建议 guzzlehttp ( Github Page)

Easy Usage:

易于使用:

Installation:

安装:

 composer require guzzlehttp/guzzle

Send request and get response:

发送请求并得到响应:

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

回答by VIshal Jamwal

As an alternate , we can use file facade. To get information from external sites, we can use guzzle. Link is https://github.com/guzzle/guzzle

作为替代,我们可以使用文件外观。要从外部站点获取信息,我们可以使用 guzzle。链接是https://github.com/guzzle/guzzle