未加星标

Post to Facebook Wall from Website using PHP

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Facebook is the most popular social media and shares on the Facebook wall are the most used activity by its user. Facebook share option is a common and required feature for every web application. We can easily share the post to Facebook by manually or using the HTML button. In this tutorial, you’ll learn how to post activity on Facebook wall from website usingphp and Facebook API .

Post to Facebook wall is useful when you want to post dynamic content to Facebook from website. Here we’ll build a simple PHP script to publish Facebook post from website using Facebook PHP SDK . This functionality lets the user submit post (message, picture, link, text content) on their Facebook timeline from website using PHP and Facebook API .

Before getting started to post to Facebook wall using PHP, take a look at the files and folders structure.


Post to Facebook Wall from Website using PHP
Facebook Apps Creation

To access Facebook API, App ID & App Secret need to be specified on Facebook API call. You need to create a Facebook App for generating App ID & App Secret. If you’re not already created a Facebook app, visit the link below to create and configure a Facebook App from the App Dashboard .

Creating a Facebook App and get App ID & App Secret.

After completing the Facebook App creation and configuration you’ll get the App ID and App secret. Copy this App ID and App Secret of your Facebook App for later use.

Facebook PHP SDK

All Facebook PHP SDK files are included in the inc/ directory, place the inc/ folder into the root directory. You can get all Facebook PHP SDK files in our source file ZIP.

Facebook API Configuration (fbConfig.php)

The fbConfig.php file is used to configure and call Facebook API. Specify your Facebook App ID ( $appId ) and App Secret ( $appSecret ) to connect FB API.

<?php //Include Facebook SDK require_once 'inc/facebook.php'; /* * Configuration and setup FB API */ $appId = 'InsertAppID'; //Facebook App ID $appSecret = 'InsertAppSecret'; // Facebook App Secret //Call Facebook API $facebook = new Facebook(array( 'appId' => $appId, 'secret' => $appSecret )); ?> Submit Post to Facebook Wall (index.php)

Include the fbConfig.php file to connect Facebook API and get FB user ID.

If FB user ID is available, the following will happen.

Determines the access token that should be used for API calls. Post message, name, link, description, and the picture will be submitted to Facebook wall. Post submission status will be shown.

If FB user ID is not available, the Facebook Login URL will be generated and the user would be redirected to the FB login page.

<?php //Include FB config file require_once 'fbConfig.php'; //Get FB user $fbUser = $facebook->getUser(); if($fbUser){ $accessToken = $facebook->getAccessToken(); //FB post content $message = 'Test message from CodexWorld.com website'; $title = 'Post From Website'; $link = 'http://www.codexworld.com/'; $description = 'CodexWorld is a programming blog.'; $picture = 'http://www.codexworld.com/wp-content/uploads/2015/12/www-codexworld-com-programming-blog.png'; $attachment = array( 'access_token' => $accessToken, 'message' => $message, 'name' => $title, 'link' => $link, 'description' => $description, 'picture'=>$picture, ); try{ //Post to Facebook $facebook->api("/me/feed", "post", $attachment); //Display post submission status echo 'The post was submitted successfully to Facebook timeline.'; }catch(FacebookApiException $e){ echo $e; } }else{ //Get FB login URL $redirectURL = 'http://localhost/nitya/facebook_post_from_website/'; $fbLoginURL = $facebook->getLoginUrl( array( 'redirect_uri' => $redirectURL, 'scope' => 'publish_actions' ) ); //Redirect to FB login header("Location:".$fbLoginURL); } ?>

Note that: The access token must have the publish_actions permission to post on Facebook wall.

Conclusion

We’ve tried to provide a simple way to share the post to Facebook wall from website using PHP . Hope! our script will help you to post on Facebook wall from your website. Also, you can change the post content dynamically by specifying the respective value in $attachment array.

Download Source Code

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: FacebookPHPHTML
分页:12
转载请注明
本文标题:Post to Facebook Wall from Website using PHP
本站链接:http://www.codesec.net/view/534536.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(72)