Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the alternative for file_get_contents in Laravel?

Tags:

php

laravel

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

File is available at this URL

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

this simple PHP function is working and retrieving the file

$content = file_get_contents($url);

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

"File does not exist at path 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;
like image 383
user3680417 Avatar asked Jan 01 '19 06:01

user3680417


People also ask

What is file_get_contents in laravel?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is the difference between file_get_contents () function and file () function?

file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.

What will the file_get_contents () return?

Return Values ¶ The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.

Is file_get_contents secure?

Unfortunately, file_get_contents() is a dangerous function when used with data obtained from user input, as it is the case. Using it could allow diverse kinds of attacks, from Denial of Service to loading of remote malicious resources.


1 Answers

I suggest 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();
like image 161
Saman Ahmadi Avatar answered Oct 12 '22 03:10

Saman Ahmadi