Search code examples
phpfile-get-contentsimdb

Can't get content with file_get_contents function


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!


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