Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file content from URL?

When I use following URL in browser then it prompt me to download a text file with JSOn content.

https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json

(Click above URL see downloaded file content)

Now I want to create a php page. I want that when I call this php page, it should call above URL and get content(json format) from file and show it on screen.

How can I do this ??

like image 738
Awan Avatar asked Apr 02 '11 10:04

Awan


People also ask

How do I find the contents of a file?

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.

How do I echo content in PHP?

readfile("/path/to/file"); This will read the file and send it to the browser in one command. This is essentially the same as: echo file_get_contents("/path/to/file");

What does file_get_contents return?

The file_get_contents() function returns Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking the specified offset in the stream fails.


2 Answers

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')); 

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); ?> 

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

like image 143
John Parker Avatar answered Sep 29 '22 17:09

John Parker


1) local simplest methods

<?php echo readfile("http://example.com/");   //needs "Allow_url_include" enabled //OR echo include("http://example.com/");    //needs "Allow_url_include" enabled //OR echo file_get_contents("http://example.com/"); //OR echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb"  //needs "Allow_url_fopen" enabled ?>  

2) Better Way is CURL:

echo get_remote_data('http://example.com'); // GET request  echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); // POST request 

It automatically handles FOLLOWLOCATION problem + Remote urls:
src="./imageblabla.png" turned into:
src="http://example.com/path/imageblabla.png"

Code : https://github.com/tazotodua/useful-php-scripts/blob/master/get-remote-url-content-data.php

like image 30
T.Todua Avatar answered Sep 29 '22 16:09

T.Todua