আগামী ২৩ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

Working with Facebook API Part-2: Posting Messages on Your Facebook Wall and Fan Pages

Introduction

In this article we will learn some tips on how to manipulate posts on facebook through an app using the  PHP sdk: posting messages, upload and tag photos , sending photos to fanpage and directly into albums .

So you can use the scripts below it takes you to create an application on facebook. If you are unsure of how to create apps for facebook, visit my previous article that deals with this subject.

 Publish Posts On Your Wall

The script below is a facebook application that simply publish posts directly to the user timeline

<?php

// Include the fo lib facebook

require('facebook-php-sdk-master/src/facebook.php');

// Create the instance of the application, stating the appid and secret

$facebook = new Facebook(array(

'appId'  => '236076976577688',

'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',

));

//Get the id of the User

$user = $facebook->getUser();;

if($user) { // User logged

try  {

// Check if the User permitiou the application on your profile publish photos

$permissions = $facebook->api("/me/permissions");

if(!array_key_exists('publish_stream', $permissions['data'][0])) {

header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );

exit;

}

// Send data to the publication of the picture

$post_data = array(

'message'  => "Most Popular Online Tutorials For World Wide Web Programmers" ,

'name'  => "w3programmers" ,

'link'  => "http://www.w3programmers.com/" ,

);

// Publish posts on the timeline

$data['photo'] = $facebook->api("/me/feed", "post", $post_data);

echo  "post published successfully" ;

} catch  (FacebookApiException $e ) {

// Exception handling

echo ( $e );

$user  = null;

}

} else  {

// User not logged in, request authentication

$loginUrl  = $facebook -> getLoginUrl ();

echo  "<a href =$loginUrl>Facebook Login </ a> <br />" ;

echo  "You <strong> not connected .. </ em> </ strong>" ;

}

Some points to be highlighted in the above script:

  • To request permission publish_stream so that the application can publish posts in the user profile.
// Check if the User permitiou the application on your profile publish photos

$permissions = $facebook->api("/me/permissions");

if(!array_key_exists('publish_stream', $permissions['data'][0])) {

header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );

exit;

}

  •  The major fields are messagename and link.
$post_data = array(

'message'  => "Most Popular Online Tutorials For World Wide Web Programmers" ,

'name'  => "w3programmers" ,

'link'  => "http://www.w3programmers.com" ,

);

Description of fields

  • Link: Link attached in the publication
  • name: Name of the link
  • message: The message contained in the publication

Now Run Your Script and See the results Below:

Submit Post On Facebook Using App

Linking an image in post

See the previous post does not parameterize any images. The mechanism of post own facebook chose a “relevant” image contained in informed on the post link. But by the sdk lib, we can determine which will be attached to the post picture. To do so, inform the field picture. Let’s change the previous example source.

See Updated code below:

// Send data to the publication of the picture

$post_data = array(

'message'  => "Most Popular Online Tutorials For World Wide Web Programmers" ,

'name'  => "w3programmers" ,

'link'  => "http://www.w3programmers.com",

'picture'  => "http://www.w3programmers.com/wp-content/uploads/2014/01/elephant.jpg" ,

);

The only difference here is the field picture that was added. It tells what is the link for an attached image in post. Valley attend here, the picture will not be published on the user’s facebook profile. Is it just included a link to an accessible external image through a URL.

Below New Post Output with Image Link:

Linking an image in post

See which now define the image that will be displayed along with the post. Unlike the previous post that the mechanism of facebook itself chose an image to be attached.

Submit a video post

Now we will see how to incorporate the posting, a link to the video. Where is a link to a video on youtube:PHP Tutorial # 1 . To this end, we use the parameter source . Let’s change the previous example source.

// Send data to the facebook User Feed

$post_data = array(

'message'  => "Basic PHP Tutorial on Youtube" ,
'name'  => "PHP Video Lecture" ,

'link'  => "http://www.youtube.com/watch?v=Txj4mj5yom0",
'source'  => "http://www.youtube.com/e/Txj4mj5yom0 ",
'caption' =>"www.youtube.com",

);

Note that in the above code, we inform the parameter source that should be a direct link to the video.This video can be watched within Facebook itself. The URL defined in parameter link will open in a new window when you click the link in the post. This case will open the youtube page referring to video lesson.

Below New Video Post Result

Submit Video Post

Notice also, that last post, we use a new parameter called caption . The caption is displayed below the name (field name) Posting link. Instead of displaying the link www.youtube.com/watch?v=Txj4mj5yom0 , appears only www.youtube.com

 Direct publishing photos in album

In the previous examples we have seen how to publish photos directly to the user profile. The photos will be published in an album that contains the same name as the application of facebook. However, you can specify the picture is published directly to a specific album. The first step is to have already created this album and be with his ID in hand.

Get the id of the album is quite simple. Enter the area of ​​profile photos, edit your album and get the id of the open URL in the browser:

Album Id For Facebook App Id

In the above case, the id of the album will be: 138467043179

The line of code below is mandatory if you are uploading files for your application.

// Enables support for uploading files

$facebook -> setFileUploadSupport (true);

With the id of the album at hand, now just use it in the call to publish photos. Replace the line below:

// Send data to the facebook User Feed

$post_data = array(

'message'  => "Basic PHP Tutorial on Youtube" ,

'name'  => "PHP Video Lecture" ,

'link'  => "http://www.youtube.com/watch?v=Txj4mj5yom0",

'source'  => "http://www.youtube.com/e/Txj4mj5yom0 ",

'caption' =>"www.youtube.com",

);

$data['photo'] = $facebook->api("/me/feed", "post", $post_data);

echo  "post published successfully" ;

by:

// Send data to the publication of the picture

$post_data = array (

"message" => "teste_" . time (),

"image" => '@' . realpath ( "1.jpg" ), // photo path

);

// Publish posts on the timeline

$data['photo'] = $facebook->api("/138467043179/photos", "post", $post_data);

echo  "post published successfully" ;

Now Run Your Script and See the results Below:

Direct publishing photos in album

Publishing photos on a fanpage

For those unaware, is a fanpage created a custom page within facebook. It is a way to promote your business, your company, products, bands, artists, etc. …

You can post photos directly through a fanpage on facebook application. The user should be used in the application administrator fanpage. Also it should allow the application post on your page by allowing manage_pages .

We are assuming you have already created your fanpage. Therefore, the first step to be able to post photos in it, is to get your id. Open fanpage page, click Edit Page -> Update info . see you fanpage id below:

Facebook Page Id For App Development

In the example above, the ID was fanpage: 453435918118366

Let the next steps:

  • Update the necessary permissions to publish to fanpage ( publish_stream and manage_pages )

 

// Check if the User permitiou the application on your profile publish photos

$permissions = $facebook->api("/me/permissions");

if(!array_key_exists('publish_stream', $permissions['data'][0])

||! array_key_exists ( 'manage_pages' , $permissions [ 'data' ] [0])) {

header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream,manage_pages")) );

exit;

}

 Then get the access_token of the fanpage. We must inform the access_token at the time of publication of the picture. For the token, we need to inform the id of the fanpage.

 

/* Get token */

$fanpage_id = "453435918118366"; // id of the fanpage

$page_info = $facebook ->api("/$fields = access_token fanpage_id?" ); // requesting the token

$fanpage_token = $page_info [ 'access_token' ]; // token

 Now run your script and see output below:

Publishing photos on a fanpage

Reference
For more options regarding posting messages on facebook, go to the official documentation:http://developers.facebook.com/docs/reference/api/post/

Create a facebook application for posting Messages on your facebook wall and fan pages

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I graduated in 2009 with a bachelor's degree in Engineering from State University Of Bangladesh, I'm also a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, CIDA and MAX Group where I worked on ERP software and web development., but now i'm a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services Ltd. I'm also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute - a leading Training Institute in the country.
One comment on “Working with Facebook API Part-2: Posting Messages on Your Facebook Wall and Fan Pages

Leave a Reply

Your email address will not be published. Required fields are marked *