Viewed   125 times

When I use following URL in browser then it prompt me to download a text file with JSOn content.,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 ??



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

$jsonData = json_decode(file_get_contents(',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:

    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, ',40&chl=Hello|World&chof=json');
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

    $jsonData = json_decode(curl_exec($curlSession));

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

Wednesday, October 26, 2022

A few years ago I benchmarked the two and CURL was faster. With CURL you create one CURL instance which can be used for every request, and it maps directly to the very fast libcurl library. Using file_get_contents you have the overhead of protocol wrappers and the initialization code getting executed for every single request.

I will dig out my benchmark script and run on PHP 5.3 but I suspect that CURL will still be faster.

Friday, December 16, 2022

It seems you need define an user agent. Try this;

$url = "";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080311 Firefox/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);


If you still insist on using file_get_contents then it is still possible to use user agent;

$url = "";

$options = array(
        "header"=>"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10rn" // i.e. An iPad

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

For more information you can check file_get_contents and stream_context_create (for using headers) documentation

Saturday, November 5, 2022

Instead of reinventing the wheel, how about using Apache commons-io:


public class FilenameUtilTest {

    public static void main(String[] args) throws Exception {
        URL url = new URL("");

        System.out.println(FilenameUtils.getBaseName(url.getPath())); // -> file
        System.out.println(FilenameUtils.getExtension(url.getPath())); // -> xml
        System.out.println(FilenameUtils.getName(url.getPath())); // -> file.xml

Wednesday, August 3, 2022

Use async task:

private class ReadTask extends AsyncTask<String, Integer, String> 

    protected String doInBackground(String... params) 

        return "success";

To invoke it use:

new ReadTask().execute(<your url>);
Sunday, August 21, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :