Facebook graph api and file_get_contents problem


Hi, i try to create a simple script that fetch userprofil data from an id

Example: here the id is zuk

$link = json_decode(file_get_contents('http://graph.facebook.com/zuck'));
echo $link_>name;

Problem is, it’s seems like facebook limit this kind of request, sometime it’s working but sometime i get the error :

Warning: file_get_contents(http://graph.facebook.com/zuck) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Do you know how to bypass this ?

Thank you.

Make sure in your PHP ini file you have allow_url_fopen set to On or it will block cross domain requests.

Just did a phpinfo() : allow_url_fopen On On

I think the problem is on the facebook side. Butit’s orking time by time …

I ran the script on my local server and it worked fine, which version of PHP are you using?

PHP Version 5.2.17

Did you try it several time ? i mean try to refresh the page ?

I refreshed it 30 times within 1 minute and go a JSON response back every time, sounds like something isn’t configured right or you’re hitting facebook.com too many times in a certain time period.

I tried on another server and it’s woking fine. Thanks for your help. I have another question, how to manage error with this ? if i type a bad username for example ?

î will get a 404 not found and my script will stop

Typically an error property would be returned instead of graph data so you can look for that once you have parse the response.