How to use PHP SDK for Facebook Posting

How to use PHP SDK for Facebook Posting

PHP_development-large

Facebook is a leading social media website that houses over 1.1 billion active users and 665 million daily active users over the world. Facebook is preferred by the businesses for its cost-effective marketing strategy. It is one of the best social marketing website for better customer engagement.

Marketing experts believe that ‘Facebook’ posting is the key to social marketing.

Facebook Posts through Application:

    • For Facebook posting, the first thing we need is the User Access Token.
    • We can get the Access Token when user connects Facebook account to the application.
    • There are two types of page where we can post.1.    Personal Page2.    Fan Page
    • To post on Fan page, we need to get the Page Access Token using the User Access Token.
    • An API call to https://graph.facebook.com/me/accounts?access_token={USER_ACCESS_TOKNE}
    • Facebook PHP SDK: https://github.com/facebook/facebook-php-sdk/tree/master/src

Here I have mentioned some major types of Facebook postings.

Status Update

Update your status or write on your wall in plain text.
We can use “\n\n” for line break.

[sourcecode]$post_type = ‘/$page_id/feed’;
$post_params = array(
‘message’ => “Andolasoft, solutions delivered.\n\n Ruby on Rails Development | CakePHP Development | Android and iPhone Application Development”
);[/sourcecode]

Share Link

Share a link on Facebook. The link can be a website, news feed, YouTube or an image.
Facebook will get the OG (open graph) title, description and image of that link and post it as a link share to the wall.

[sourcecode]$post_type = ‘/$page_id/feed’;
$post_params = array(
‘message’ => “We’re Hiring!”,
‘link’ => “http://www.andolasoft.com/”
);[/sourcecode]

When Facebook finds multiple images, it would choose the first image and post it as a thumbnail.
We can also provide the thumb image to display on the link share.

Note: Facebook use the thumb image greater than size 200*200

[sourcecode]$post_params = array(
‘message’ => “We’re Hiring!”,
‘link’ => “http://www.andolasoft.com/”,
‘picture’ => “http://blog.andolasoft.com/wp-content/themes/aware/images/blog_as_logo.png”
);[/sourcecode]

Upload a Photo

Other than image link share, we can upload a photo to Facebook wall.
We need to upload the photo to a Facebook album. There is a default album “timeline” for the wall photos.

To get the timeline album use FQL,

[sourcecode]$fql = “SELECT object_id FROM album WHERE owner=$page_id AND type=’all’”
$fql = urlencode($fql);
$fql_query_url = ‘https://graph.facebook.com/fql?q=’ . $fql . ‘&’ . $access_token . ‘&format=json-strings’;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
$wall_album = isset($fql_query_obj[‘data’][0][‘object_id’]) ? $fql_query_obj[‘data’][0][‘object_id’] : $page_id;[/sourcecode]

You have to provide the relative path of image to upload.
That image might be a link, but save it in your local file system and provide the relative path.

[sourcecode]$imgSrc = ‘/var/www/html/andolasoft/logo.jpg’;
$post_type = ‘/$wall_album/photos’;
$post_params = array(
‘name’ => “Based in Silicon Valley, we design, develop iPhone, iPad, Android & Web application for startups & established businesses”,
‘photo’ => ‘@’.realpath($imgSrc)
);[/sourcecode]

Upload a Video

Other than share a YouTube video, we can also upload video to Facebook.
The video must be in the local file system to be uploaded to Facebook.

[sourcecode]$videoSrc = ‘/var/www/html/andolasoft/tour.flv’;

$post_type = ‘/me/video’;
$post_params = array(
‘title’ => “Andolasoft”,
‘name’ => “Based in Silicon Valley, we design, develop iPhone, iPad, Android & Web application for startups & established businesses”,
‘photo’ => ‘@’.realpath($videoSrc),
‘picture’ => ‘http://blog.andolasoft.com/wp-content/themes/aware/images/blog_as_logo.png’
);[/sourcecode]

Post to Facebook using the Facebook PHP SDK.

[sourcecode]$facebook = new Facebook(array(
‘appId’ => APP_KEY,
‘secret’ => SECREATE_KEY
));

try{
$facebook->setAccessToken($access_token);
$fbpost = $facebook->api($post_type, ‘POST’, $post_params);
} catch (Exception $e) {
echo $e->getMessage();
}[/sourcecode]

Conclusion:

Basic permissions required for above operations are,

    • publish_stream
    • manage_pages
    • photo_upload
    • offline_access

Access Token might invalidate for following reasons,

    • The token expires after the allotted expire time
    • The default expire time of Facebook access token is 2 hours, but we can get the extended Access Token up to 60 days.
    • The user changes her password which invalidates the access token.
    • The user de-authorizes your app.

See More: Facebook Login Migration Using PHP SDK

Tags:
,
Avatar
Mahavir S. Senapati
mahavir.senapati@andolasoft.co.in