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
5 |
require ( 'facebook-php-sdk-master/src/facebook.php' ); |
9 |
$facebook = new Facebook( array ( |
11 |
'appId' => '236076976577688' , |
13 |
'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6' , |
19 |
$user = $facebook ->getUser();; |
27 |
$permissions = $facebook ->api( "/me/permissions" ); |
29 |
if (! array_key_exists ( 'publish_stream' , $permissions [ 'data' ][0])) { |
31 |
header( "Location: " . $facebook ->getLoginUrl( array ( "scope" => "publish_stream" )) ); |
41 |
'message' => "Most Popular Online Tutorials For World Wide Web Programmers" , |
43 |
'name' => "w3programmers" , |
45 |
'link' => "http://www.w3programmers.com/" , |
51 |
$data [ 'photo' ] = $facebook ->api( "/me/feed" , "post" , $post_data ); |
53 |
echo "post published successfully" ; |
55 |
} catch (FacebookApiException $e ) { |
69 |
$loginUrl = $facebook -> getLoginUrl (); |
71 |
echo "<a href =$loginUrl>Facebook Login </ a> <br />" ; |
73 |
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.
3 |
$permissions = $facebook ->api( "/me/permissions" ); |
5 |
if (! array_key_exists ( 'publish_stream' , $permissions [ 'data' ][0])) { |
7 |
header( "Location: " . $facebook ->getLoginUrl( array ( "scope" => "publish_stream" )) ); |
- The major fields are message, name and link.
3 |
'message' => "Most Popular Online Tutorials For World Wide Web Programmers" , |
5 |
'name' => "w3programmers" , |
7 |
'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:
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:
5 |
'message' => "Most Popular Online Tutorials For World Wide Web Programmers" , |
7 |
'name' => "w3programmers" , |
9 |
'link' => "http://www.w3programmers.com" , |
11 |
'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:
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.
5 |
'message' => "Basic PHP Tutorial on Youtube" , |
6 |
'name' => "PHP Video Lecture" , |
8 |
'link' => "http://www.youtube.com/watch?v=Txj4mj5yom0" , |
9 |
'source' => "http://www.youtube.com/e/Txj4mj5yom0 " , |
10 |
'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. I think it is also a good investment that I will buy more youtube views from my videos soon.
Below New Video Post Result
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:
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.
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:
5 |
'message' => "Basic PHP Tutorial on Youtube" , |
7 |
'name' => "PHP Video Lecture" , |
9 |
'link' => "http://www.youtube.com/watch?v=Txj4mj5yom0" , |
11 |
'source' => "http://www.youtube.com/e/Txj4mj5yom0 " , |
13 |
'caption' => "www.youtube.com" , |
17 |
$data [ 'photo' ] = $facebook ->api( "/me/feed" , "post" , $post_data ); |
19 |
echo "post published successfully" ; |
by:
5 |
"message" => "teste_" . time (), |
7 |
"image" => '@' . realpath ( "1.jpg" ), |
13 |
$data [ 'photo' ] = $facebook ->api( "/138467043179/photos" , "post" , $post_data ); |
15 |
echo "post published successfully" ; |
Now Run Your Script and See the results Below:
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:
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 )
3 |
$permissions = $facebook ->api( "/me/permissions" ); |
5 |
if (! array_key_exists ( 'publish_stream' , $permissions [ 'data' ][0]) |
7 |
||! array_key_exists ( 'manage_pages' , $permissions [ 'data' ] [0])) { |
9 |
header( "Location: " . $facebook ->getLoginUrl( array ( "scope" => "publish_stream,manage_pages" )) ); |
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.
3 |
$fanpage_id = "453435918118366" ; |
5 |
$page_info = $facebook ->api( "/$fields = access_token fanpage_id?" ); |
7 |
$fanpage_token = $page_info [ 'access_token' ]; |
Now run your script and see output below:
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.