49

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

0

5 Answers 5

94

Depending on your PHP configuration, this may be a easy as using:

$url = 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json'
$content = file_get_contents($url);

And then you can process the loaded data. For example, JSON:

$jsonData = json_decode($content);

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.

4
  • 1
    Thank you voted up :) edit: I tried without json_decode and got a fully rendered url but with decode, nothing. What is the purpose of decode?
    – user5340092
    Commented Mar 27, 2018 at 22:50
  • 1
    @Steven It decodes JSON string into a PHP variable. Commented Mar 31, 2018 at 9:37
  • @John_Parker many thanks. I'm currently using Google to render a snapshot of a url but it's not ideal as the image is small but with JSON decode as a PHP variable, is this an array I can split and grab things like a page title and the first image? Cheers.
    – user5340092
    Commented Apr 1, 2018 at 6:20
  • 1
    From PHP 5.1.3, BINARYTRANSFER option has no effect: the raw output will always be returned when CURLOPT_RETURNTRANSFER is used. php.net/manual/en/function.curl-setopt.php
    – Nadav
    Commented Jan 3, 2021 at 10:41
25
  1. local simplest methods

     <?php
     readfile("http://example.com/"); //needs "allow_url_fopen" enabled
     //OR
     include("http://example.com/"); //DANGEROUS! needs "allow_url_include" enabled 
     //OR
     echo file_get_contents("http://example.com/"); //needs "Allow_url_fopen" enabled
     //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

1
  • sorry how do I call it with REPLACESSOURCES = True?
    – Madthew
    Commented Aug 5, 2019 at 6:22
4

Don't forget: to get HTTPS contents, your OPENSSL extension should be enabled in your php.ini. (how to get contents of site use HTTPS)

3

Use file_get_contents in combination with json_decode and echo.

2
  • I am using file_get_contents to get Content but it is not JSON when I echo it. It is showing some special characters.
    – Awan
    Commented Apr 2, 2011 at 10:44
  • @Awan were you ever able to figure this out? I'm also seeing special characters.
    – jewel
    Commented Aug 2, 2013 at 0:34
2
$url = "https://chart.googleapis....";
$json = file_get_contents($url);

Now you can either echo the $json variable, if you just want to display the output, or you can decode it, and do something with it, like so:

$data = json_decode($json);
var_dump($data);

Not the answer you're looking for? Browse other questions tagged or ask your own question.