Facebook Graph API for PHP

php

Like the old REST API, you don’t need to add 2-3 files for Facebook PHP SDK. The new graph api comes in a single file which is located at http://github.com/facebook/php-sdk/

Copy the facebook.php file in to your webroot and include this file in your php code.

require_once(‘facebook.php’);

To work fully functional, you need CURL and JSON installed in your server. To check this use phpinfo() function.

To make sure, these missing is the main problem, add  exit(‘Curl error’); just above the line   throw new Exception(‘Facebook needs the CURL PHP extension.’); in facebook.php file.

Same for JSON.

The Linux way of installing CURL is :

sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

To install JSON in your linux machine, follow these steps:

  1. pecl install json
  2. Add json.ini file in /etc/php.d/
  3. edit json.ini (in VI) and add this line: xtension=json.so
  4. Save the file and restart appache (/etc/init.d/httpd restart)  or apache2 restart depends on your linux OS

See the example.php file in php-sdk from Github, if you call getSession function, you only get a NULL string if you are not logged in proper way. So I recommend to redirect into login page if there is no session available.

Here is the change:

if (! $facebook->getSession()) {

header(‘Location: ‘.$facebook->getLoginUrl());

}

Then try the rest of the part as per example.php , it will work

Good Luck

Sajith

3 Comments , , , , , , , , , , , , , , ,

3 Responses to “Facebook Graph API for PHP”

  1. Jennie Lofaro August 12, 2010 at 6:48 pm #

    lol,I am searching for my love,this post is very useful.

  2. techjuck October 20, 2010 at 10:04 pm #

    Wonder why it tookme this long to get to this article. I thought the google was good at finding better results. Ok now, i just want to say that you’ve put up a nice and descriptive post. Regards

  3. FS November 13, 2011 at 9:21 pm #

    Hello, i believe that i noticed you visited my blog thus i came to return the desire?.I am trying to find things to enhance my web site!I suppose its adequate to make use of some of your ideas!!

Leave a Reply

More in general (9 of 34 articles)


If you aim to create a multi-language  website using PHP + Mysql , remember the following tips. You can ...