I am using the following PHP code to post to my page wall. it is okay I can post successfully as admin but I want to post to the wall using the page name (page account). how do I post using the page name or page id. I tried to find good resource to explain Facebook API function but i did not find good documentation for it.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}");
exit;
}
else {
echo "i am connected";
}
$attachment = array('message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
?>
I found the solution to the problem.
To post or to do any task as a page , you need an access token.
Here is my final code after the update, I want to share it with you because I found many people having difficulty with getting the correct access code for a page.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 123456789){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
?>
My Facebook OAuth is a little rusty, but I believe this will help:
http://bugs.developers.facebook.net/show_bug.cgi?id=10005
It is the exact bug you are describing, but I have no way of testing that it corrects the issue specifically.
This should help: http://developers.facebook.com/docs/reference/api/page/#feed
It looks like you need the manage_pages permission.
Related
I'm using FB App to publish posts to my FB pages.
So when i use this code for links, it works.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true
));
$attachment = array(
'access_token' => $row["token"],
'name' => $_POST["name"],
'picture' => $_POST["picture"],
'message' => $_POST["message"],
'link' => $_POST["link"]);
try
{
$facebook->api("/".$row['id']."/feed", "POST", $attachment);
}
catch(Exception $e)
{
echo('exception caught '.$e->getMessage());
}
But, when i want to share pictures as a page:
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
'fileUpload' => true,
'allowSignedRequest' => false
));
$attachment = array('access_token' => $row["token"], 'url' => 'http://image.link.jpg', 'message' => $_POST["message"]);
try
{
$facebook->api("/".$row['id']."/photos", "POST", $attachment);
}
catch(Exception $e)
{
echo('exception caught '.$e->getMessage());
}
The app post picture as user. USER to PAGE.
So i found that i need to use publish_pages permission, but my access token contains both publish_actions and publish_pages. I can't change that because I'm using one access token for a lot of things.
So i have to ask, is there a way to explicitly choose what permissions i want to use from access token. FB API have same code for page and user picture publish /user_id/photos and /page_id/photos.
Need to use Page access token, there's no other way.
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
I am trying to publish a checkin using facebook api. Sometimes, it works well and posts the checkin, but mostly 99% of the times it produces the error: "This webpage has a redirect loop"
checkin.php:
<?php
require("../src/facebook.php");
// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXX',
'cookie' => false
));
$token = $facebook->getAccessToken();
//echo $token;exit())
try {
// to get the id of the currently logged in user
// if, you want you can manually set a user id here like this:
//$uid = '[FB USER ID]';
$uid = $facebook->getUser();
$facebook->setAccessToken($token);
$facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '101697613295949',
'message' => 'Enjoying Chill Beer with Team',
'picture' => 'http://test.com/someplace.png',
'coordinates' => json_encode(array(
'latitude' => '28.541203543000023',
'longitude' => '77.15503053709995',
'tags' => 'XXXX'))
));
echo 'You are checked in';
} catch (Exception $e){
// No user found - ask the person to login
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
?>
Thanks in advance.
how to create an event of facebook using facebook application?
further i want to use graph api for that purpose,kindly help me out...
$access_token = 'app_access_token';
$url = "https://graph.facebook.com/app_id/events";
$postdata = "access_token=$access_token&name=$name&start_time=$start_time&end_time=$end_time&description=$description&privacy=$privacy&location=$location&picture=$picture_source";
$event_code = curl_sending_newsfeed_facebook($url, $postdata);
i have already done the creation of event but picture of event is not shown (i mean a picture represents the face of event)
try doing something like this,
$fb = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
'cookie' => false,
'fileUpload' => true
));
$imageFile = realpath('/path/to/your/example.jpg');
$eventInfo = array(
'name' => 'some title',
'start_time' => time()+32400,
'description' => 'some description',
basename($file) => '#'.$imageFile
);
$result = $fb->api(
'/'.$app_id.'/events',
'post',
$eventInfo
);
echo 'Created event #'.$result['id'];
Further to Post to users wall upon facebook app submission (my old question), I have came up with the following code however it doesn't seem to be working?? I thought best to open a new question as it is a new question.
What am I doing wrong? Also, where should this code go?
<?php
$session = $facebook->getSession();
//Is user logged in and has allowed this app to access its data
if (!$session) {
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'next' => 'enter.php',
'cancel_url' => 'index.php',
));
// use the $loginUrl created on the enter button to request permission;
}
$user_id = $facebook->getUser();
//post to wall
$attachment = array('message' => '<message>',
'name' => '<name here>',
'caption' => '<caption here>',
'link' => '<link to app>',
'description' => '<enter description >',
'picture' => '<enter image url>',
'actions' => array(array('name' => '<enter action label>',
'link' => '<enter action url>')
);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
try {
$post_id = $facebook->api('/me/feed', 'post', $attachment);
} catch (CurlException $e) {
//timeout so try to resend
$post_id = $facebook->api('/me/feed', 'post', $attachment);
}
catch (FacebookApiException $e) {
error_log($e);
}
} else {
// We don't have the permission
// Alert the user or ask for the permission!
}
// store the post id in-case you need to delete later
?>
I'll just post the code I'm using that works. Hope it helps
fbClass.php
public function __construct() {
// Naredimo instanco
$facebook = new Facebook(array(
'appId' => $this->fbid,
'secret' => $this->fbsecret,
'cookie' => true,
));
$this->facebook = $facebook;
}
function authUser($facebook) {
$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;
}
}
// Login or logout url will be needed depending on current user state.
if (!($user)) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me, user_birthday, email, publish_stream',
'redirect_uri' => 'http://apps.facebook.com/myappname/',
));
echo("<script> top.location.href='" . $loginUrl . "'</script>");
} else {
return true;
}
}
process.php
$facebook = $fbClass->facebook;
$fbAuth = $fbClass->authUser($facebook);
if ($fbAuth) {
$res = $facebook->api('/me/feed/', 'post', array(
'message' => MESSAGE,
'name' => NAME,
'caption' => '',
'description' => DESC,
'picture' => PIC,
'link' => 'http://www.facebook.com/myapp/',
'actions' => array('name' => 'Test', 'link' => 'http://apps.facebook.com/myapp/')
));
}
You need a Facebook access token for this code to work. Add your token where My Access token here is in the following code:
$attachment = array(
'access_token' => 'My Access token here',
'message' => '',
'name' => 'My Wall Post Header/Title Here',
'caption' => 'Small caption here',
'link' => 'http://www.mywebsite.org',
'description' => 'Wall Post Details Here',
'picture' => "http://www.mywebsite.org/images/logo.gif",
);
You can get access tokens here.