There are different ways to connect with your audience. You can create a website, a blog, or any other digital presence to amplify your message. But there are also social media sites that make it easier to reach people directly. In fact, Facebook is the most popular social networking site in the world. According to Statista, by the end of this year, it had more than 1.86 billion monthly active users, which makes it an ideal platform for brands and businesses to promote their products and services. We’ll show you how you can use PHP SDK for Facebook posting so that your brand has a voice on this prominent platform. This article provides insight into what you need to get started as well as helpful tips along the way.
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.
1 2 3 4 | $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” ); |
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.
1 2 3 4 5 | $post_type = '/$page_id/feed'; $post_params = array( 'message' => “We're Hiring!”, 'link' => “https://www.andolasoft.com/” ); |
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
1 2 3 4 5 |
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,
1 2 3 4 5 6 | $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; |
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.
1 2 3 4 5 6 | $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) ); |
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.
1 2 3 4 5 6 7 8 9 | $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), ); |
Post to Facebook using the Facebook PHP SDK.
01 02 03 04 05 06 07 08 09 10 11 | $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(); } |
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