Question

I want to get movie Rating from IMDB website. For example, I have a url http://p.media-imdb.com/static-content/documents/v1/title/tt1520211/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json Run it and you can see the movie rating is 8.7 and I wan to get it, so I used this query:

$s="http://p.media-imdb.com/static-content/documents/v1/title/tt1520211/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json";
$s = file_get_contents($s);
$s=explode('rating":',$s);
$s=explode(',"rating',$s[1]);
echo $s[0];

But it doesnt work and I don't know why!

Was it helpful?

Solution

I am checking the script out but uhm ye your array contains nothing check it with: print_r($s) the output is:

Array ( [0] => )

updated

When you show the content it shows:

‹5KOÃ0„ÿJµ'šÄvÞ9U‚.‰³›LÁ¢y`o*UUþ;v ·õÎìÌg3ôÇØj6ãgl—ñáF‡¬©¹Ñ4#ÓH µ†ÏxÛlŽödñ³ÀñKï¥4Óe*€èˆ2¤DÕŠH×U‘zÔ"‡?q°Ó¡5^5i\gÕ’Ü´Ø¡ðÀ×Ùd“žMÌ¡õŸ.ԚЗlÛ„YæJ()/l«€ù…݇>{ÿKí_0_Þa BÔÚR£„{êôè¿æ lx¤­š*.ï§iÙC—…Rb]1“$æ6

so your explode will also be checked to that string.

Code:

$s="http://p.media-imdb.com/static-content/documents/v1/title/tt1520211/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json";
$s = file_get_contents($s);
/*$s=explode('"rating":',$b);
$s=explode('"rating":',$b[1]);
print_r($s);*/

print($s);

updated: also check the code with JSONLint

it seems like it has an error

I think you can find more in this question for your answer: json to php array using file get contents

OTHER TIPS

I assume the PHP.ini setting allow_url_fopen is disabled, which means that remote URIs (ie http(s), etc) can not be opened using the file family functions (fopen,..)

Please also refer to its config value description according to which it is a system wide setting, so I think you can not override it using ini_set().

Alternatively you could use the cURL extension, for details on how to use it please refer to its documentation.

Please run php info to check if the allow_url_fopen is set to ON

php_info();

But use the CURL for getting the content, which simulating an actual web client. Do not turn on this option, because it's not a good for security purposes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top