Update Facebook wall with PHP - php

I have a blog system where I want the user to be able to post on there Facebook wall without them having to login to Facebook. I would like it to happen through the PHP SDK delivered by Facebook.
This is the permissions I have given the Facebook application i created:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxx&redirect_uri=http://www.mydomain.com/facebook/index/&scope=offline_access,publish_stream,read_stream,manage_pages
Just to clarify the permissions: offline_access,publish_stream,read_stream,manage_pages
Below codes only works if the user is logged in. else I get this error:
FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
public function facebook()
{
require_once('facebook/php-sdk-3.0.0/src/facebook');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://www.google.com',
'description' => 'Test of application PHP',
'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'http://www.google.com')
)
);
$result = $facebook->api('/me/feed/','post',$attachment);
print_r($facebook->getAccessToken());
}

In order to post to someone's wall your application must have wall post permissions for that user. In order to get those permissions the user must login to facebook and grant you access.
For more info: http://developers.facebook.com/docs/authentication/

Also, you need "offline_access" if you want to post when's the user is not logged in.

Related

Facebook PHP SDK V3 login token - Fatal error: Uncaught OAuthException:

The code below works fine with the PHP SDK v3. However, I had to use a java sdk login at first and now I don't know how to get a token with PHP or check if the user is logged in or not with PHP. I GET THE ERROR BELOW.
I can not use the SDK 4 yet as my hosting will not allow it. Long story. I have tried SEVERAL sites and tutorials for the past few hours to no avail. Can someone show me a simple way to do this? With code examples? What do I need to change the canvas url to on the Facebook app? etc..
Here is an error I get without the login.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in
require 'Facebook3/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'myID',
'secret' => 'MySecret',
));
$attachment = array(
'message' => 'COOL',
'name' => 'Some Info',
'caption' => "more info",
'link' => 'http://aLink.com',
'description' => 'View Trailer Here!',
'picture' => 'http://Picture.com/images/aPic.png',
'actions' => array(
array(
'name' => 'Get Search',
'link' => 'http://www.google.com',
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);

Why can't I add an event as a page?

I'm trying to add an event to a page in the name of the page. I'm the admin of the page and I found a lot of stuff to the topic, but, no matter what I do, it ends in the PHP Exception:
Uncaught Exception: You must be an admin of the specified page to perform the requested action.
I can add events with my app in my own profile. So, I think the problem is the Access Token. I tried several ways, but to me the best way seems to be:
Open the Graph API Explorer
Select my Application
Request a new access code with permissions manage_pages, publish_actions, publish_stream, create_event
Request /me/accounts
Get the access_token from the page I want to post to and use it in my script
My test script, that works in my own profile:
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => $APPLICATION_ID,
'secret' => $APPLICATION_SECRET,
'cookie' => false,
'fileUpload' => true
));
$event_id = 0;
$event_name = "New Event API Test Event";
$event_start = '2013-12-04';
$event_privacy = "OPEN";
$ret_obj = $facebook->api('/'.$page_id.'/events?access_token='.$access_token, 'POST',
array(
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'location_id' => $location_id
));
if(isset($ret_obj['id'])) {
echo 'Event with added. ID: ' . $ret_obj['id'];
} else {
echo 'Couldn\'t create event';
}
What I did wrong?
You need a page access token.
Page Access Token – These access tokens are similar to user access
tokens, except that they provide permission to APIs that read, write
or modify the data belonging to a Facebook Page. To obtain a page
access token you need to start by obtaining a user access token and
asking for the manage_pages permission. Once you have the user access
token you then get the page access token via the Graph API.
Doc: https://developers.facebook.com/docs/facebook-login/access-tokens/
Then use GET /me/accounts to get the access token associated to your page.
I found the solution in How to programmatically add an event to a page using Graph API?. I had to add the access_token direct into the event array and everything works as expected:
$ret_obj = $facebook->api('/'.$page_id.'/events', 'POST',
array(
'access_token' => $access_token,
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'location_id' => $location_id
));
Thanks for the help!

Facebook Graph Api - Posting to page without logging

I'm trying to create a script that post status updates on facebook pages without logging-in the user. I've got access_token for my application and for the pages as the user granted the application once; but I always get the error
Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action
despite I've granted manage_pages and publish_stream permission.
Also if I use the access_token for the page I get the error
Fatal error: Uncaught OAuthException: Error validating access token: The session is invalid because the user logged out.
Here is the code I use.
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXXXXXXXXXX'
));
$post = array(
'message' => 'Message to user'/*,
access_token = "XXXXXXXX"*/
);
$feed = '/[PAGE_ID]/feed';
$post_id = $facebook->api($feed, "post", $post);
Thanks.
Edit :
I finally managed to post status updates on a user's page while this user was disconnected, I had to generate the access_token through the server-side auth method.
Below is a small code smaple on how to do a FB post to a page:
yOU NEED:
Your_authToken_for_User
List item
saved_fb_Page_Token
$facebook = $facebook = new Facebook(array(
'appId' => $fbAppId,
'secret' => $fbAppSecret,
'cookie' => true,
));
$facebook->setAccessToken("Your_authToken_for_User");
//Now your FB instance is authenticated.
$pages= $facebook->api('/me/accounts'); //use the current authenticated FB instance to grab Pages. If you already know your page ID , skip this.
//Assuming you have a pageid in $page_id,(the $page_id can be grabbed from the $pages array).
$attachment = array(
'access_token' => $saved_fb_Page_Token,
'message' => "foo",
'name' => "Bar",
'link' => "http://foo.bar",
'description' => "barfoo",
'picture' => "an image url here",
);
$facebook->api('/' . $page_id . '/feed', 'POST', $attachment);//post to fb page

Facebook post wall data when facebook offline ( access_token = staticValue )

I am trying to post dynamic data to facebook wall.
I downloaded facebook api for php.
First of all, as for testing, I use below code.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$config = array(
'appId' => 'myAPPID',
'secret' => 'MySecretCode'
);
//'access_token' => $access_token = $facebook->getAccessToken(),
$facebook = new Facebook($config);
$attachment = array(
'access_token' => 'my_tokenkey',
'message' => 'Test Message offline test message',
'name' => 'Name Test offline test...',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'caption' => 'this is caption for action link',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
);
try {
$facebook->api('/me/feed', 'POST', $attachment);
} catch(FacebookApiException $e) {
echo $e;
# handling facebook api exception if something went wrong
}
?>
As for my program requirement, I want my program to post data to facebook's wall whether facebook is online or offline.
But when I try this code, I get error.
OAuthException: An active access token must be used to query information about the current user.
Could anyone please give me suggestions?

How to post on a facebook application user's facebook feed using cURL?

I have made a snake game using javascript and HTML5 Canvas element. It can be found at apps.facebook.com/snaqe_game. I have attained permissions and authorized the app for publish_feed or something using the Facebook Developer Documentation but the problem I am facing is while posting the player's score on his wall.
Please Note:
I do not have the PHP SDK
Please See: apps.facebook.com/mg-copter
Update Facebook News Feed using the Graph API in PHP
$data = array();
$data['post'] = array(
'access_token' => $access_token,
'message' => 'this is the message',
'picture' => 'http://sudocode.net/wp-content/uploads/2011/01/developer.facebook.com_banner-150x150.png',
'link' => 'http://sudocode.net/article/313/how-to-register-a-new-facebook-application/',
'name' => 'How to Register a New Facebook Application',
'caption' => 'sudocode | it looks like it works',
'description' => 'Here are some notes on how to register a new Facebook application.',
'actions' => '{"name": "Read the Article", "link": "http://sudocode.net/article/313/how-to-register-a-new-facebook-application/"}',
);
$response = xhttp::fetch("https://graph.facebook.com/{$profile_id}/feed", $data);

Categories