views:

90

answers:

1

I want to get the like_count of a page. I tried using PHP SDK, but I've given up on it because my site always stops loading after the line of code where the FQL query is executed, so I wanted to try to get the like count 'manually'.

I tried using file_get_contents() on https://api.facebook.com/restserver.php?method=fql.query&query=QUERY&format=json, but it shows me nothing.

Do you know how I can get the JSON string from the above URL on which I can use json_decode(), or even a better way to get the like count?

Any help is appreciated.

PS If it means anything, QUERY is SELECT like_count FROM link_stat WHERE url="page_url_here"

A: 

Your FQL should be encoded, and you can use new url address:

https://api.facebook.com/method/fql.query?query=SELECT%20like_count%20FROM%20link_stat%20WHERE%20url%3D%22http%3A%2F%2Fsample.com%22&format=json&access_token=<ACCESS_TOKEN>
serg
I tried that, I added this code:$json = 'https://api.facebook.com/method/fql.query?query='.rawurlencode('SELECT like_count from link_stat WHERE url="my_url"').' echo $json."<br/>"; var_dump(file_get_contents($json));And var_dump shows bool(false).Any other suggestions?
Michael Lorence
@Michael You probably need to pass access token as a parameter (I edited the answer). Do you have it?
serg
No. I once saw how I can get it through facebook api url and passing my app id and secret but I can't find it now. Can you tell me how to obtain the access token?
Michael Lorence
serg
I edited the code as you said, but still bool(false)
Michael Lorence
@Michael Copy your url into browser and see what it says. Are you sure that fql is correct? Copy your fql into test console and it will show you url that was used to run it: http://developers.facebook.com/docs/reference/rest/fql.query
serg
The first thing I did was check if the fql is ok several times. It is correct, it worked when I used fql with javascript, only I can't get it to work on PHP.
Michael Lorence
@Michael So when you copy fql url into the browser it works (log out from facebook first) but when you open exactly the same url from php it doesn't?
serg
Yes, can it be a problem because I'm using file_get_contents? Should I use some other method?btw thank you for your fast support.
Michael Lorence
@Michael I don't know. PHP says that if file_get_contents returns false it means error occurred. Maybe try other methods - fopen and curl module.
serg
Could you, please, give me the code to use with curl?And is there a way I could see the error that occurred with file_get_contents?
Michael Lorence
@Michael curl: http://ca.php.net/manual/en/function.curl-setopt.php
serg