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 

1 <?php
2  
3 // Include the fo lib facebook
4  
5 require('facebook-php-sdk-master/src/facebook.php');
6  
7 // Create the instance of the application, stating the appid and secret
8  
9 $facebook new Facebook(array(
10  
11 'appId'  => '236076976577688',
12  
13 'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',
14  
15 ));
16  
17 //Get the id of the User
18  
19 $user $facebook->getUser();;
20  
21 if($user) { // User logged
22  
23 try  {
24  
25 // Check if the User permitiou the application on your profile publish photos
26  
27 $permissions $facebook->api("/me/permissions");
28  
29 if(!array_key_exists('publish_stream'$permissions['data'][0])) {
30  
31 header( "Location: " $facebook->getLoginUrl(array("scope" => "publish_stream")) );
32  
33 exit;
34  
35 }
36  
37 // Send data to the publication of the picture
38  
39 $post_data array(
40  
41 'message'  => "Most Popular Online Tutorials For World Wide Web Programmers" ,
42  
43 'name'  => "w3programmers" ,
44  
45 'link'  => "http://www.w3programmers.com/" ,
46  
47 );
48  
49 // Publish posts on the timeline
50  
51 $data['photo'] = $facebook->api("/me/feed""post"$post_data);
52  
53 echo  "post published successfully" ;
54  
55 } catch  (FacebookApiException $e ) {
56  
57 // Exception handling
58  
59 echo $e );
60  
61 $user  = null;
62  
63 }
64  
65 else  {
66  
67 // User not logged in, request authentication
68  
69 $loginUrl  $facebook -> getLoginUrl ();
70  
71 echo  "<a href =$loginUrl>Facebook Login </ a> <br />" ;
72  
73 echo  "You <strong> not connected .. </ em> </ strong>" ;
74  
75 }

Some points to be highlighted in the above script:

1 // Check if the User permitiou the application on your profile publish photos
2  
3 $permissions $facebook->api("/me/permissions");
4  
5 if(!array_key_exists('publish_stream'$permissions['data'][0])) {
6  
7 header( "Location: " $facebook->getLoginUrl(array("scope" => "publish_stream")) );
8  
9 exit;
10  
11 }
1 $post_data array(
2  
3 'message'  => "Most Popular Online Tutorials For World Wide Web Programmers" ,
4  
5 'name'  => "w3programmers" ,
6  
7 'link'  => "http://www.w3programmers.com" ,
8  
9 );

Description of fields 

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:

1 // Send data to the publication of the picture
2  
3 $post_data array(
4  
5 'message'  => "Most Popular Online Tutorials For World Wide Web Programmers" ,
6  
7 'name'  => "w3programmers" ,
8  
9 'link'  => "http://www.w3programmers.com",
10  
11 'picture'  => "http://www.w3programmers.com/wp-content/uploads/2014/01/elephant.jpg" ,
12  
13 );

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 the video has gotten lots of views thanks to the https://www.socialmediadaily.com/youtube/gain-subscribers services. To this end, we use the parameter source . Let’s change the previous example source.

1 // Send data to the facebook User Feed
2  
3 $post_data array(
4  
5 'message'  => "Basic PHP Tutorial on Youtube" ,
6 'name'  => "PHP Video Lecture" ,
7  
8 'link'  => "http://www.youtube.com/watch?v=Txj4mj5yom0",
9 'source'  => "http://www.youtube.com/e/Txj4mj5yom0 ",
10 'caption' =>"www.youtube.com",
11  
12 );

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. I think it is also a good investment that I will buy more youtube views from my videos soon.

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.

1 // Enables support for uploading files
2  
3 $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:

1 // Send data to the facebook User Feed
2  
3 $post_data array(
4  
5 'message'  => "Basic PHP Tutorial on Youtube" ,
6  
7 'name'  => "PHP Video Lecture" ,
8  
9 'link'  => "http://www.youtube.com/watch?v=Txj4mj5yom0",
10  
11 'source'  => "http://www.youtube.com/e/Txj4mj5yom0 ",
12  
13 'caption' =>"www.youtube.com",
14  
15 );
16  
17 $data['photo'] = $facebook->api("/me/feed""post"$post_data);
18  
19 echo  "post published successfully" ;

by:

1 // Send data to the publication of the picture
2  
3 $post_data array (
4  
5 "message" => "teste_" . time (),
6  
7 "image" => '@' realpath "1.jpg" ), // photo path
8  
9 );
10  
11 // Publish posts on the timeline
12  
13 $data['photo'] = $facebook->api("/138467043179/photos""post"$post_data);
14  
15 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. Just visit the page, it is a way to promote your business, your company, products, bands, artists, etc. You can apply for business loan to start your own business, why not check here for more information.

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:

 

1 // Check if the User permitiou the application on your profile publish photos
2  
3 $permissions $facebook->api("/me/permissions");
4  
5 if(!array_key_exists('publish_stream'$permissions['data'][0])
6  
7 ||! array_key_exists 'manage_pages' $permissions 'data' ] [0])) {
8  
9 header( "Location: " $facebook->getLoginUrl(array("scope" => "publish_stream,manage_pages")) );
10  
11 exit;
12  
13 }

 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.

 

1 /* Get token */
2  
3 $fanpage_id "453435918118366"// id of the fanpage
4  
5 $page_info $facebook ->api("/$fields = access_token fanpage_id?" ); // requesting the token
6  
7 $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’m 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, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company 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.