Grabbing permission from facebook user and posting to their wall - php

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.

Related

upload image when creating face event with graph api php

I dont seem to be able to get this to work. The event loads fine but the image just doesnt upload, please let me know what I am doing wrong, I thiink there is a problem with the picture url format, does in need the # in front?, I have tried it without and it doesn't work:
////////////////////////////////////////////////////////////////////////////////////////
require 'facebook.php';
// appId and secret all work fine
$facebook = new Facebook(array(
'appId' => '************',
'secret' => '**************',
'fileUpload' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$response = $facebook->api(
"/me/events",
"POST",
array (
'name' => $title,
'start_time' => $start_time,
'location' => $location,
'end_time' => $end_time,
'description' => $description,
'picture' => '#http://andyotto.files.wordpress.com/2013/01/donald- duck.gif?w=863',
'venue' => $venue
)
);
if (count($response)>0){
$response_message = "Event successfully added uploaded to Facebook.";
}else{
$response_message = "There was a problem uploading your event to Facebook.";
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope'=>'create_event'
));
header( 'Location:'.$loginUrl ) ;
}
It seems from the docs that you can't upload an image directly once you publish an Event (https://developers.facebook.com/docs/graph-api/reference/user/events/#publish), at least the picture filed is not described.
According to this doc (https://developers.facebook.com/docs/graph-api/reference/event#picture) you can set the Event's picture once it's created via
POST /{EVENT_ID}/picture
with the picture encoded as "multipart/form-data" in the request's body parameter "source".

Redirect loop when trying to publish a checkin via Facebook API

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.

Facebook session not being created

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;
.
.
.

Problem with posting to Facebook Page using PHP script as Page account

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.

Publish to Facebook Stream via PHP using Graph API

I'm trying to post a message to a user's wall using the new graph API and PHP. Connection seems to work fine, but no post appears. I'm not sure how to set up the posting code correctly. Please help me out. Sorry for the broken-looking code, for some reason StackOverflow didn't want to close it all in the code block.
Below is my full code. Am I missing an extender permission requests, or is that taken care in this code:
PHP Code
<?php
include_once 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true
));
$session = $facebook->getSession();
if (!$session) {
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
} else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated;
$connectUrl = $facebook->getUrl(
'www',
'login.php',
array_merge(array(
'api_key' => $facebook->getAppId(),
'cancel_url' => 'http://www.test.com',
'req_perms' => 'publish_stream',
'display' => 'page',
'fbconnect' => 1,
'next' => 'http://www.test.com',
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',
), $params)
);
$result = $facebook->api(
'/me/feed/',
'post',
array('access_token' => $facebook->access_token, 'message' => 'Playing around with FB Graph..')
);
} catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}
}
?>
it's because you didn't sepcify the "req_perms" and allow the app to publish stream to your wall. Make sure you add "publish_stream" in your "req_perms.
PHP code:
$url = $facebook->getLoginUrl(array(
'canvas' => 1, //set to 1 bcoz my application is Iframe app
'fbconnect' => 0,
'req_perms' => 'publish_stream'
));

Categories