Can anyone tell me how to do this using php sdk.
I have using this code. This code was suppose to post to user's timeline as well as the fan page. The person who is logged in would be the one who is doing the post. So on the fan page
this would have appeared under recent activity. However, now the code has stopped working saying that the user has not authorised the app. I do not understand why this could be happening.
include("../php-sdk/facebook.php");
/*START FACEBOOK lOGIN*/
$facebook = new Facebook(array(
'appId' => Appid,
'secret' => Appsecret,
'cookie' => true
));
$pageId = PageId;
$user= $facebook->getUser();
$newfbuser = 0;
if (!empty($user)) {
$uid = $facebook->getUser();
// $fb_access_token = $facebook->getAccessToken();
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream, manage_pages'
));
$user = $facebook->api('/me');
$fb_access_token = $facebook->getAccessToken();
$param = array(
'method' => 'users.getInfo',
'uids' => $uid,
'fields' => array('name','sex')
);
$users_getinfo = $facebook->api($param);
$save['oauth_uid'] = $users_getinfo['0']['uid'];
$save['oauth_provider'] = 'facebook';
$save['facebook_email'] = $users_getinfo['0']['email'];
$save['name'] = $users_getinfo['0']['name'];
$link_url = "http://google.com";
$pic = 'http://xxxxx/images/Testing.jpg';
$attachment = array(
'access_token' =>$fb_access_token,
'message' => 'This is a message by bob',
'link' => 'http://xxxxx/');
// print_r($fb_access_token);
echo 'on my timeline<br />';
$facebook->api("/me/feed",'post',$attachment);
$facebook->api("/$pageId/feed",'post',$attachment);
echo 'successfully posted';
}
// $fb_access_token = $facebook->getAccessToken();
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream, manage'
));
No such thing call "manage", the correct permission was manage_pages
Related
I want to use PHP to send message to my Facebook friends. I found some code but it just lets me log in. Could anyone help me?
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXXX',
'cookie' => true,
));
$session=$facebook->getUser();;
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
$access_token = $facebook->getAccessToken();
try {
$result = $facebook->api(
'me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
1) Did you provided the correct permissions to the app?
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
2) Check this answer: Update facebook status using php and this: Facebook PHP SDK , Post to another users wall whats wrong?
3) it's /me/feed, not me/feed:
$result = $facebook->api(
'/me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
I get from facebook some code to auto sharing , so i need to edit my code to get , auto sharing several times , not only 1 time....i need a help in how to make that issue , i let for you my code:
//-- Facebook API --//
require_once 'php-sdk/facebook.php';
//-- App Information --//
$app_id = 'zzzzzzzzz';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxx';
$pageId= 'me';
// Create Facebook Instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//-- To Facebook (Notice we ask for offline access) --//
if (empty($_REQUEST))
{
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'offline_access,publish_stream'
));
header('Location:'.$loginUrl );
}
//-- From Facebook --//
else
{
$user = $facebook->getUser();
if($user)
{
$access_token = $facebook->getAccessToken();
$req = array(
'access_token' => $access_token,
'message' => 'Jeremy Gibbs is a handsome lad!');
//-- Send post to Facebook --//
$res = $facebook->api('/'.$pageId.'/feed', 'POST', $req);
header("location: http://facebook.com/".$pageId."");
}
}
so can u tell me how ?
You can user cron jobs on your server OR use http://twitterfeed.com/
I am using this code to post to the user's wall :
require 'fb/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'redirect_uri' => 'urlhere',
);
$fb_session = $facebook->getUser();
// Session based API call.
if ($fb_session)
{
try
{
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
}
catch (Exception $e)
{
$fb_login_url = $facebook->getLoginUrl($params);
echo $e->getMessage();
}
}
else
{
$fb_login_url = $facebook->getLoginUrl($params);
}
The code is working till the point uuer clicks on login part to post. After that the url contains a code = xxxzz and a state =yyy but the $fb_session is 0.
This works sometimes without any changes. Please help!
You should include the access token when you try to post to some User Facebook wall.
First get access token:
$fbToken = NULL;
$fbToken = $facebook->getAccessToken();
right after $fb_session = $facebook->getUser();
and then include it in the $args array:
'access_token' => $fbToken
if session is NOT created or access token is not valid, you should redirect the user back to re-authorize your Facebook App.
So in ELSE, you should add
header("Location: $fb_login_url ");
after $fb_login_url = $facebook->getLoginUrl($params);
assuming that everything else is correct, but the access_token is not valid, you should also include header("Location: $fb_login_url "); before echo $e->getMessage();
in both cases you are using getloginurl
shouldn't it be
if ($fb_session) {
try {
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
} catch (Exception $e) {
$fb_logout_url = $facebook->getLogoutUrl;
.
.
.
i have made this script.
It works perfect and post a photo to my facebook fanpage.
But after one hour, the login token is invalid. How can i make a permanent login to my FB Fanpage.
Any Idea?
<?php
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'fileUpload' => true
));
$access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$facebook->setAccessToken($access_token);
$facebook->setExtendedAccessToken();
$params = array(
'access_token' => $access_token
);
// The id of the fanpage
$fanpage = 'XXXXXXXXXXXXXX';
// Get the page access token
$accounts_list = $facebook->api('/me/accounts');
foreach($accounts_list['data'] as $account){
if($account['id'] == $fanpage){
$fanpage_token = $account['access_token'];
echo "<p>Page -- Access Token:<br>$fanpage_token</p>";
}
}
$args = array(
'source' => '#'.realpath('img/17226_014.jpg'),
'message' => 'php sdk test',
'access_token' => $fanpage_token
);
$post_url = '/'.$fb_page_id.'/photos';
$photo = $facebook->api($post_url, 'post', $args );
if( is_array( $photo ) && ! empty( $photo['id'] ) ) {
echo 'Photo uploaded. Check it on Graph API Explorer. ID: ' . $photo['id'];
}
?>
You need to get the new extented access token with
<?php
$new_access_token = $facebook->getAccessToken();
?>
after you call $facebook->setExtendedAccessToken();
And of course you need to use that access token for your next API calls.
Here is My code :
<?
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/config.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/fonctions.php');
include '../api/facebook.php';
include '../api/xhttp.php';
$config = array();
$config['appId'] = '35455XXXXXXX207';
$config['secret'] = '6006855f3aXXXXXXXXX9bce45a426';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$args = array(
'message' => 'Hello from my App!',
'link' => 'http://www.masteringapi.com/',
'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!',
'access_token' => $access_token
);
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$post_id = $facebook->api("/me/feed", "post", $args);
?>
But it returns : Uncaught OAuthException: An active access token must be used to query information about the current user.
Any ideas ?
Thanks!
Here is My new code :
<?
$config = array();
$config['appId'] = '3545XXXXXXX6207';
$config['secret'] = '60068XXXXXXXXXXXe45a426';
$facebook = new Facebook($config);
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
if(!isset($_GET['session'])) {
$params = array(
'locale' => 'fr_FR',
'display' => 'popup',
'req_perms' => 'email,user_about_me,user_birthday,user_location',
'ext_perms' => 'publish_stream,share_item,manage_pages'
);
header("Location: ".$facebook->getLoginUrl($params));
}
$args = array(
'message' => "XXXX",
'link' => 'XXXX',
'name' => 'XXXX',
'picture' => 'XXXX',
"caption"=> "lien.com",
"type"=> "link");
$post_id = $facebook->api("/me/links", "post", $args);
?>
This time i get this error : OAuthException: (#282) Requires extended permission: share_item thrown
Any ideas ?
Thanks!
You're not passing it the access token.
Check out getAccessToken().
From your code sample it looks like you are trying to use the variable $access_token to create your $args array, before you define the $access_token variable. I would switch the order of variable definitions:
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$args = array(
'message' => 'Hello from my App!',
'link' => 'http://www.masteringapi.com/',
'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!',
'access_token' => $access_token
);
This is assuming $retour_login['access_token'] contains a valid Facebook access token. Otherwise you will need to fetch the current user's access token using the Facebook SDK:
$access_token = $facebook->getAccessToken();