Facebook allows for convenient use of its Graph API from within PHP. So, let’s have a look at what needs to be done to get things up and running! Therefore, this article describes the creation of a simple example web page, which just handles authentication and posts a message to the logged in user’s wall.
- a facebook account.
- webspace with PHP.
Create a facebook app: In order to do anything programmatically with facebook, be it connecting a serverside PHP page, an iPhone app or directly using Graph API URLs in a web browser,
you will need to set up a facebook app first.
- Read this article for a step-by-step howto. We’ll just need an app set up and retrieve its
App Secret. So, if you don’t intend to integrate your website within your facebook page, skip the steps for publishing your app as a facebook tab.
- Go to your app’s summary screen: Under http://developers.facebook.com, click on
Appsand select your newly created app. There you’ll find your app’s
App Secret, which we’ll need later on.
- Provide your website URL: In your app’s summary screen click on
Weband put your website’s URL in
Download the facebook API: Download the facebook PHP API from here and unpack it onto your webspace.
Use facebook’s PHP SDK to post to a user’s wall: If not already done, create a PHP page on your webspace at the location where your facebook app settings point at. Then, just paste following snippet into your PHP page and replace
YOUR_FACEBOOK_SDK_PATH_HEREwith the path on your server where you placed the facebook API.
YOUR_APP_ID_HEREwith your app’s
YOUR_APP_SECRET_HEREwith your app’s
YOUR_MESSAGEwith a message string that will appear on top of the image in your generated post.
YOUR_PICTURE_URLwith an absolute URL to the image that should be displayed.
YOUR_LINKwith the URL that should open up once someone clicks on the post’s link name or image.
YOUR_LINK_NAMEwith the name (visible string) of the link.
YOUR_CAPTIONwith a description that will appear below the link name.
In case the user is not yet logged in, the following code will redirect the user to facebook’s login dialog and ask him for the
publish_stream permission, which is necessary for doing wall posts. After successful login, a post will be generated on the logged in user’s wall.
The resulting post will look similar to this example, which is taken from our breakout-style iPhone game Action Block Buster:
This was just an example use of the facebook PHP SDK for accessing the Graph API. Other API methods may be accessed the same way:
- Display information about the currently logged in user
- Display currently logged in user’s status messages
- Upload a photo
// Allow file uploading first.
// Add photo to facebook.
and so on…
Copy from http://nocturnsoft.com/devblog/?p=1745