Wall posts with the facebook php sdk


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.

This article is a more practical approach to my previous article “wall posts with the facebook graph api“, which described how the REST-based Graph API works in general, but didn’t make use of any of the available facebook SDKs. In this article, we’ll stick with the same example (posting a message to a user’s wall), but we’ll be approaching it from an application developer’s point of view.

Requirements:

  • 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 ID and 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 Apps and select your newly created app. There you’ll find your app’s App ID and App Secret, which we’ll need later on.
  • Provide your website URL: In your app’s summary screen click on Edit, select Web and put your website’s URL in Site URL and Site Domain.

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_HERE with the path on your server where you placed the facebook API.
  • YOUR_APP_ID_HERE with your app’s App ID.
  • YOUR_APP_SECRET_HERE with your app’s App Secret.
  • YOUR_MESSAGE with a message string that will appear on top of the image in your generated post.
  • YOUR_PICTURE_URL with an absolute URL to the image that should be displayed.
  • YOUR_LINK with the URL that should open up once someone clicks on the post’s link name or image.
  • YOUR_LINK_NAME with the name (visible string) of the link.
  • YOUR_CAPTION with a description that will appear below the link name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
require_once "YOUR_FACEBOOK_SDK_PATH_HERE/src/facebook.php";
$app_id = "YOUR_APP_ID_HERE";
$app_secret = "YOUR_APP_SECRET_HERE";
// Init facebook api.
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));
// Get the url to redirect for login to facebook
// and request permission to write on the user's wall.
$login_url = $facebook->getLoginUrl(
    array('scope' => 'publish_stream')
);
// If not authenticated, redirect to the facebook login dialog.
// The $login_url will take care of redirecting back to us
// after successful login.
if (! $facebook->getUser()) {
    echo <<< EOT
<script type="text/javascript">
top.location.href = "$login_url";
</script>;
EOT;
    exit;
}
// Do the wall post.
$facebook->api("/me/feed", "post", array(
    message => "YOUR_MESSAGE",
    picture => "YOUR_PICTURE_URL",
    link => "YOUR_LINK",
    name => "YOUR_LINK_NAME",
    caption => "YOUR_CAPTION"
));
?>

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
    1
    2
    $me = $facebook->api("/me");
    var_dump($me);
  • Display currently logged in user’s status messages
    1
    2
    $statuses = $facebook->api("/me/statuses");
    var_dump($statuses);
  • Upload a photo
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // Allow file uploading first.
    $facebook->setFileUploadSupport(
        "http://" . $_SERVER["SERVER_NAME"]
    );
    // Add photo to facebook.
    $photo = $facebook->api("/me/photos", "POST", array(
        source => "@" . "YOUR_IMAGE_PATH",
        message => "YOUR_MESSAGE"
    ));
    var_dump($photo);

and so on…

Copy from http://nocturnsoft.com/devblog/?p=1745

Advertisements
By dbglory Posted in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s