Asked  7 Months ago    Answers:  5   Viewed   60 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, March 31, 2021
answered 7 Months ago

According to the PHP manual, you must specifically encode a URL if it contains special characters. This means the function itself should do no special encoding. Most likely your URL is being encoded before being passed to the function, so pass it through urldecode first and see what happens.

Edit: You're saying the encoding is being messed up. Again the PHP manual specifically states that you need to encode urls prior to passing them to file_get_contents. Try encoding the URL, then passing it to the function.

$url = urlencode($url);
Friday, May 28, 2021
answered 5 Months ago

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, May 29, 2021
answered 5 Months ago

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

Saturday, June 12, 2021
answered 5 Months ago

It looks like you are using Python 3, and have read some code / documentation for Python 2.x. It is poorly documented, but there is no getheaders method in Python 3, but only a get_all method.

See this bug report.

Tuesday, September 28, 2021
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :