Facebook API post to friends wall - facebook

I'm building application to post statuses from authenticated users like that one.
The problem is the application posting from my profile on friends wall who authenticated the application from my profile.
I'm using Facebook API with CodeIgniter.
the code
$userId = $this->facebook->getUser();
$IDs = $this->facebook_data->get_IDs();
if(isset($_POST['link'])){
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'XXXX','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status2'],
'link' => $_POST['link']
);
}else{
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'Resala','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status1']
);
}
foreach($IDs->result() as $row){
$ID=$row->user_id;
$this->facebook->api("/$ID/feed", "post", $args);
}

The right docs for creating a post are:
http://developers.facebook.com/docs/reference/api/user/#posts
There you can see the params "from" and "application" don't exist and "name (can only be used if link is specified)".
It's not possible to post in the name of an app. You can only post in the name of an page (that may be assosiated with the app).
Therefor you have to use the page access token instead of the user access token.
See: http://developers.facebook.com/docs/reference/api/page/#page_access_tokens

Related

How to get email from facebook through Guzzle in laravel?

I am using below code to login via facebook in laravel.
Referring https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps for token based authentication and using https://github.com/sahat/satellizer for social media integration.
$params = [
'code' => $request->input('code'),
'client_id' => $request->input('clientId'),
'redirect_uri' => $request->input('redirectUri'),
'client_secret' => 'XXXXXXXXXXXXXXXXXXX'
/*'client_secret' => Config::get('app.facebook_secret')*/
];
// Step 1. Exchange authorization code for access token.
$accessTokenResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/oauth/access_token', [
'query' => $params
]);
$accessToken = json_decode($accessTokenResponse->getBody(), true);
// Step 2. Retrieve profile information about the current user.
$profileResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/me', [
'query' => $accessToken
]);
$profile = json_decode($profileResponse->getBody(), true);
$profile returning only fb id and user name.
What changes should I do to get email from facebook.
Use below code in your step 2.
$fields = 'id,email,first_name,last_name,link,name';
$profileResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/me', [
'query' => [
'access_token' => $accessToken['access_token'],
'fields' => $fields
]
]);
Replace this:
'https://graph.facebook.com/v2.5/me'
with this:
'https://graph.facebook.com/v2.5/me?fields=name,email'
It´s called "Declarative Fields", see changelog.
Also, you need to authorize with the email permission, of course. And the email has to be confirmed. You can´t be 100% sure to get an email, some users use their phone number to login.
You should also check this out: https://github.com/sahat/satellizer/issues/615

is it possible to post facebook page wall using a cron job?

Please don't vote and flag this quest. Please.
I am is the admin of the page. How can I post to that page wall using php SDK.
Is it possible or not?
the following code is posting as I post the page. I want the page itself post things to the wall.
<?php
include_once("inc/facebook.php"); //include facebook SDK
######### edit details ##########
$appId = '1157979879888106'; //Facebook App ID
$appSecret = '86403d4061433e23a0ouo9ec46a1405'; // Facebook App Secret
$return_url = 'http://xxxx.com/site/test'; //return url (url to script)
$homeurl = 'http://xxxx.com/site/index'; //return to home
$fbPermissions = 'publish_stream,manage_pages'; //Required facebook permissions
##################################
$page_id = "447126725331764";
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
//$fbuser = $facebook->getUser();
$publish = $facebook->api('/'.$page_id.'/feed', 'post',
array('access_token' => 'CAAQdLHAnOgoBAGAYZCIE09nfXaWZBXdMuwW5I379TjaWMZAQSUZCks9B4JfQyMXBEDN1CQZBaQJFeP44vWTGhic8AnkBgARge0AKJHIYgMdfDk16rq6ON496t0Phv6QtCmOR7t3wcybioNjxFkYctuN1ppDEBQdAObi6eIva0ZBTvAHJOWCSfcNicaVIZD',
'message'=> "Hello boss",
'from' => $appId,
'to' => $page_id,
'caption' => 'Caption',
'name' => 'Name',
'link' => 'http://www.example.com/',
'picture' => 'http://www.phpgang.com/wp-content/themes/PHPGang_v2/img/logo.png',
'description' => 'Hello boss'.' via demo.PHPGang.com'
));
?>
First of all, publish_stream is deprecated since many years. You really need to check out the Facebook docs, they state clearly that you need publish_pages and manage_pages to post "as Page".
That being said, that exact same question gets asked a LOT on Stackoverflow, so you can easily find a lot more information with the search function. In general, this is what you need to do:
Authorize the user with publish_pages and manage_pages
(Generate an Extended User Token)
Get a Page Token with /me/accounts
User that Page Token to post to the Page wall
More information about Access Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

Facebook stream publish to wall as App/Page

i wonder if it is possible to post to a users stream/wall by using stream_publish as a page or an app. Currently I've granted my app to stream_publish on my own wall. But when I post to my stream from the app the post is displayed like I would've posted it by myself instead of having the app posted it to my wall.
I want to have the post looks like the app posted it. I already used the uid-parameter in the stream_publish params and set it to the appId ... but the post still looks like an own post.
my code:
$postData = array( $feed_dir = '/'.$fb_uid.'/feed/',
$method = 'POST',
$msg_body = array( 'access_token' => $arrUserData['access_token'],
'message' => 'here goes the message',
'name' => '-name-',
'link' => 'http://some-url',
'description' => '-description-',
'actions' => ($actions),
'uid' => $iniHandler->getIniSetting('facebook.app.id')
)
);
try {
$res = $facebook->api($feed_dir, $method, $msg_body);
}
catch (Exception $e) {
$err_str = $e->getMessage();
}
Or is ther another way to post to a wall and let the post looks like a post by an app? My users can sign up for issues in my app and my app posts some news to the users wall ... thats my aim.
You also need manage_pages permissions. Once you have those, HTTP Get me/accounts and that will be a list of pages that the logged in user is admin of. In that list will be a unique page access_token. Use that token to http post a new wall/stream item.

How do you post to the wall on a facebook page (not profile)

I have a blog site written in php and it posts new blog posts to twitter and a blog ping automatically under the hood using simple http post requests passed using php curl.
I have a facebook page for the blog site and want the updates to be posted to the wall on the page, is there a simple way to do this?
What I really want is a url and set of params to parcel up as an http post request.
Note that this is to post to the wall on a new style page not a profile.
Get PHP SDK from github and run the following code:
<?php
$attachment = array(
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://mylink.com',
'description' => 'this is a description',
'picture' => 'http://mysite.com/pic.gif',
'actions' => array(
array(
'name' => 'Get Search',
'link' => 'http://www.google.com'
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);
the above code will Post the message on to your wall... and if you want to post onto your friends or others wall then replace me with the Facebook User Id of that user..for further information look out the API Documentation.
This works for me:
try {
$statusUpdate = $facebook->api('/me/feed', 'post',
array('name'=>'My APP on Facebook','message'=> 'I am here working',
'privacy'=> array('value'=>'CUSTOM','friends'=>'SELF'),
'description'=>'testing my description',
'picture'=>'https://fbcdn-photos-a.akamaihd.net/mypicture.gif',
'caption'=>'apps.facebook.com/myapp','link'=>'http://apps.facebook.com/myapp'));
} catch (FacebookApiException $e) {
d($e);
}
Harish has the answer here - except you need to request manage_pages permission when authenticating and then using the page-id instead of me when posting....
$result = $facebook->api('page-id/feed/','post',$attachment);
You can not post to Facebook walls automatically without creating an application and using the templated feed publisher as Frank pointed out.
The only thing you can do is use the 'share' widgets that they provide, which require user interaction.
If your blog outputs an RSS feed you can use Facebook's "RSS Graffiti" application to post that feed to your wall in Facebook. There are other RSS Facebook apps as well; just search "Facebook for RSS apps"...
You can make api calls by choosing the HTTP method and setting optional parameters:
$facebook->api('/me/feed/', 'post', array(
'message' => 'I want to display this message on my wall'
));
Submit Post to Facebook Wall :
Include the fbConfig.php file to connect Facebook API and get the
access token.
Post message, name, link, description, and the picture will be submitted to Facebook wall.
Post submission status will be shown.
If FB access token ($accessToken) is not available, the Facebook Login
URL will be generated and the user would be redirected to the FB login
page.
Post to facebook wall php sdk
<?php
//Include FB config file
require_once 'fbConfig.php';
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])){
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}else{
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
//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(
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
'picture'=>$picture,
);
try{
//Post to Facebook
$fb->post('/me/feed', $attachment, $accessToken);
//Display post submission status
echo 'The post was submitted successfully to Facebook timeline.';
}catch(FacebookResponseException $e){
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}catch(FacebookSDKException $e){
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}else{
//Get FB login URL
$fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
//Redirect to FB login
header("Location:".$fbLoginURL);
}
Refrences:
https://github.com/facebookarchive/facebook-php-sdk
https://developers.facebook.com/docs/pages/publishing/
https://developers.facebook.com/docs/php/gettingstarted
http://www.pontikis.net/blog/auto_post_on_facebook_with_php
https://www.codexworld.com/post-to-facebook-wall-from-website-php-sdk/