7 Subscribers

My status

Available for hire...

ask for a free quote


I can work from anywhere in the world

How to get a Facebook access token with PHP, their documentation sucks

Before I start it would be great if people showed their own implementations in PHP or ANY other languages. I will then list it in the post and give you a link back. Please post your solutions.

After reading facebooks one page of documentation on how to gain auth for their API I was left stuck with just the curl command they supply which at the time had know idea at all on how to convert it to php-cURL. I also wanted to run the script on shared/secured servers that didn’t allow me to use the exec() php command. I couldn’t find the PHP solution on Google to get a Facebook token. So I had to figure it out for myself -.-

I have never used cURL extensivly and found it quite tricky converting facebooks original steps(shown below) into PHP steps.

curl -F type=client_cred \
-F client_id=your_app_id \
-F client_secret=your_app_secret \

After blood,sweat and tears(5mins) I finally got a server response

Facebook access token using PHP

$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "xxxxxxxxxxxxxxxxx";
$client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$response = curl_exec($curl);

If anyone knows a better or faster way that works on a standard PHP server, please tell me. Sorry you have to replace & with & in the code above, my code highlighter sucks.

Also I can’t find quick solutions for getting an access token for pretty much any language so please leave behind your quick and dirty solutions. It would be great resource to have.

Useful tip with Open Graph

Also heres another tip, to get some quick and useful information of a url on facebook simply append it to the open graph url. like this
If you found that useful scroll to the top and press my like button!

Again if you have useful or cool tip for Open Graph, comment below and I will list it in the post with full credits!

Popularity: 53% [?]

posted on Friday, October 8th, 2010 by admin in facebook

6 Responses to “How to get a Facebook access token with PHP, their documentation sucks”

  1. guido says:

    i can't discover where you say to curl which url it had to read, sorry
    can u explain?

  2. Javier says:

    great work!!! I need your help, please. I need to get an access token using the JavaScript SDK. If U know how, please tell me.

  3. Will says:

    I set up that script on my server, but it doesn't return anything but a blank page…Any idea?

  4. eddie says:

    will, try to use curl_error and curl_errno function to see whats wrong.

Leave a Reply

thomas davis