Publishing To User's Wall Without Being Online/Logged-in - Facebook Sharing Using Graph API
I suggest you start learning how Facebook Graph API works first.
- Facebook will NEVER share the user password with you!
- If you just need to give the user the possibility to share link, then just use the like plugin. You may also find more interesting social plugins to use in your website.
- If you use the like plugin, it won't open any popups and it would post the link directly to the user's wall.
- You could always use the Feed Dialog
- Start reading the Facebook Documentation
Now to post on the user's wall (on his behalf) without him being logged-in, you need the following:
- app
access_token
publish_stream
permission, NO NEED for the long-lived access token:
Enables your app to post content, comments, and likes to a user's
stream and to the streams of the user's friends. This is a superset
publishing permission which also includes publish_actions. However,
please note that Facebook recommends a user-initiated sharing model.
Please read the Platform Policies to ensure you understand how to
properly use this permission. Note, you do not need to request the
publish_stream permission in order to use the Feed Dialog, the
Requests Dialog or the Send Dialog.
Require the permission:
This can be done in multiple of ways:
Using the Login Plugin:
<div class="fb-login-button" data-show-faces="true" data-width="200" data-scope="publish_stream" data-max-rows="1"></div>
Server-side login (Redirect to the OAuth Dialog):
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID
&redirect_uri=YOUR_URL
&scope=publish_stream
&state=SOME_ARBITRARY_BUT_UNIQUE_STRING
PHP-SDK:
$loginUrl = $facebook->getLoginUrl(array("scope"=>"publish_stream"));
JS-SDK through the FB.login method:
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
FB.logout(function(response) {
console.log('Logged out.');
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'publish_stream'});
Publishing:
$USER_ID = "XXXXXXXX"; // Connected once to your APP and not necessary logged-in at the moment
$args = array(
'message' => 'Hello from app',
'link' => 'http://www.masteringapi.com/',
'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!'
);
$post_id = $facebook->api("/$USER_ID/feed", "post", $args);
Note:
While it's possible to post without the user's presence always remember Facebook recommends a user-initiated sharing model
Publish Stream from the application - for non logged in user, using Graph API, php SDK
this is just a recommend & i'm not sure this be the answer
Destroy all of the sessions (or use your browser in private mode -for Firefox )
& then visit sender page Like this
example.com/postfarm.php
maybe when you visit the page normally the app gets your access_token & tries to post as you
PS:better way
use this code & you dont need to destroy sessions anymore
include_once ('src/facebook.php');/// include sdk
////// config The sdk
@ $facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXXXXXXXX',
));
$facebook->destroySession();
try{
$post=$facebook->api('USER_ID/feed/','POST',array(
'message' => '$message',
'link'=>'http://apps.facebook.com/xxxxxxx/link.php?link=',
'picture'=> 'XXXXXXXXXX',
'name'=>'XXXXXX',
'description'=>'yes',
));
}
catch(FacebookApiException $e) {
echo $e->getType();
echo '<br />';
echo $e->getMessage();
}
Publishing to User's Facebook Wall with Facebook PHP SDK
PHP-SDK 3.1.1
To generate app and user tokens. $app_access_token & $access_token
*The user access_token if you have offline access will never expire, unless user deAuthorizes your app. You would need to use for posting to a users wall when they are offline or not interacting with your application.*
<?php
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
// Create Application Access Token
$app_access = "https://graph.facebook.com/oauth/access_token?client_id=135669679827333&client_secret=xxxxxxxxxxxxxxxxxxxxxxxx&grant_type=client_credentials";
$app_access_token = file_get_contents($app_access);
// If we have a user who is logged in, create access_token with session.
if ($user) {
$access_token = $_SESSION['fb_135669679827333_access_token'];
}
?>
How to Post facebook wall automatically with Facebook API (only with userid)
below code helps you to get permissions from user and access the user's information:
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=publish_stream,user_about_me,read_friendlists,offline_access,publish_actions,friends_photos,,user_photos". "&state=" . $_SESSION['state'];
echo("<script> window.location.href='" . $dialog_url . "'</script>");
Post to a Facebook user's wall from a page or app
It's not possible to post on the user's wall as app or page. And even if it's possible, it's not recommended!
Your best bet if you want to notify your fans about something is to post on your page's wall (as your page) where this post will appear in your fans news feed.
Also this can be done for your website fans (Likers): https://developers.facebook.com/blog/post/465/
Related Topics
PHP Variable Inside Echo 'HTML Code'
Why Doesn't This Code Simply Print Letters a to Z
How to Get All Class Names Inside a Particular Namespace
Codeigniter Routes Regex - Using Dashes in Controller/Method Names
Structure of a Serialized PHP String
Parsing JSON Array with PHP Foreach
Check If Site Is Inside Iframe
MySQL Performance - "In" Clause VS. Equals (=) for a Single Value
Should I Use Prepared Statements for MySQL in PHP Performance-Wise
PHP What Is the Best Approach to Using Xml? Need to Create and Parse Xml Responses
Authenticating in PHP Using Ldap Through Active Directory
PHP Is Not Recognized as an Internal or External Command in Command Prompt
New Csrf Token Per Request or Not
PHP Regular Expression for Strong Password Validation
Get the Last Modified Date of a Remote File
Long Polling/Http Streaming General Questions
How to Enable and Use Http Put and Delete with Apache2 and PHP