npcoder
npcoder

Reputation: 454

PHP:file_get_contents() Error

I am using file_get_contents to read json data.

My code is :

   //echo $json_url;
    $json_data = file_get_contents($json_url);

I am surprise that, the variable $json_data returns null value. When I echo the variable $json_url, it displays the correct url. Tt also displays json record when I manually enter the url in browser.

What can be error here?

Upvotes: 1

Views: 1007

Answers (2)

CodeCaster
CodeCaster

Reputation: 151594

What is the URL?

Note:

If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

Furthermore, are url fopen wrappers enabled?

Tip

A URL can be used as a filename with this function if the fopen wrappers have been enabled.

But you will see why the request failed if you enable error reporting.

Upvotes: 1

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

Try to urlencode the url first

$json_data = file_get_contents(urlencode($json_url));

Upvotes: 0

Related Questions