Facebook API (Via PHP): Post to Feed? - php

I've got the basic idea covered (I think), but can't seem to successfully post anything using Facbook's provided PHP class. Given this example, can anyone see anything that might be the problem?
// Initialize Facebook API
$fb = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
if($fb->getSession()) {
$res = $fb->api('/me/feed/', 'post', array(
'message' => 'test message',
'description' => 'testing'
));
}
var_dump($res);
When I output the contents of $res I get a face-palming NULL. Any ideas? If the above looks correct, can anyone suggest some things I might try to debug this?

In your app, when you check to see if the user is logged into Facebook, make sure you are confirming that your app requested the 'special privilege' of posting to the user's wall. You can do this by modifying your code as follows:
$url = $this->facebook->getLoginUrl(array('canvas' => 1, 'fbconnect' => 0, 'req_perms' => 'user_status,publish_stream'));
Note the req_perms key being set to a list of values including 'publish_stream'. If you don't have this privilege the feed call will fail.
Also whether this is really the problem or not, you can wrap your Facebook requests with a try-catch block to catch FacebookApiException type exceptions to get more information when you have errors. For example:
try {
$uid = $this->facebook->getUser();
$me = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
print_r($e);
}

Related

Post on Facebook without Share Prompt in PHP

Is there a possibility where I can make a button on which when I click, the contents directly get shared on facebook without showing our user the share prompt dialog box?
I referred it online and found that its possible through mobile devices:
http://www.mindfiresolutions.com/How-to-post-message-on-your-facebook-wall-without-using-Facebook-dialog-Box-1419.php
The question is can we make some sort of ajax call and get this done on web apps.
We used Following code
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'My app ID', /*Your APP ID*/
'secret' => 'My Secret ID', /*Your APP Secret Key*/
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
$response = $facebook->api(
"/me/feed",
"POST",
array (
'message' => 'This is a test message',
'link' => 'www.google.com'
/* 'picture' => '{picture}',
'caption' => '{caption}',
'description' => '{description}'*/
)
);
?>
But its returns : " Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action throw in file"
Any help would be highly appreciated.
Thanks in advance.
Of course you can using the Graph API. On the button click you just have to make a \POST call to /me/feed.
Learn more about publishing a feed via Graph API and what all parameters are available here.
Permission required: publish_stream
Using PHP SDK:
$response = $facebook->api(
"/me/feed",
"POST",
array (
'message' => 'This is a test message',
'link' => '{link}',
'picture' => '{picture}',
'caption' => '{caption}',
'description' => '{description}'
)
);
Direct HTTP Request-
POST /me/feed
Host: graph.facebook.com
message=This+is+a+test+message
...
You can check your calls in Graph API Explorer
Edit:
To ask for the required permissions:
$params = array(
'scope' => 'publish_stream'
);
$login_url = $facebook->getLoginUrl($params);
You can read more about permissions here.
Try this way: https://developers.facebook.com/docs/reference/php/
When coding the wep app, you only have to provide the App Id and the App Secret, then you should have to specify the content to be posted.
You should try also the Javascript Facebook SDK, you'll find it here: https://developers.facebook.com/docs/javascript or you could go further and try the C# SDK, this one is a little bit more complex, but you will be able to do so much more.
Have fun coding!!!
Just go to https://developers.facebook.com/docs/opengraph/
Review it and place what kind of share you want.
This is the long process to be handle so. you need to study it.

How to obtain Facebook page access_token without logging in or having user interaction

I have been reading the Facebook documentation and I must be missing something, as I just cant understand how to get the access token for a page without actually logging in first. I am trying to create a PHP function using the PHP facebook API so that when I add new stories or tutorials on my site, my site's apps can then automatically post as the page a blurb about them on myy facebook page.
I have this function working but only when I get the access token from the Graph API Explorer, though the access tokens expire in about an hour. I can't seem to figure out how to programatically obtain the access_token for the page and query for a new one each time from within my PHP scripts so they don't expire and do not require user interaction.
function post_to_facebook($title, $message, $link, $picture) {
require '../facebook/src/facebook.php';
$page_token = 'xxx';
$page_id = 'xx';
$facebook = new Facebook(array(
'appId' => '<app_id>',
'secret' => '<app_secret>',
'cookie' => false,
));
$facebook->setAccessToken($page_token);
try {
$ret_obj = $facebook->api('/'.$page_id.'/feed', 'POST', array(
'caption' => $title,
'link' => $link,
'message' => $message,
'picture' => $picture
));
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
return false;
}
return true;
}
Can someone explain how I can go about retrieving the access token without manually having to look it up via graph api explorer or having a user login?
It's not possible.
The only correct (and legal) way to achieve the access token is with user interaction (through login process).

Post to fan page as admin worked yesterday, not today

To post to facebook fan page, using a php script which is called by a cronjob, im using the following code, which worked yesterday while testing, it is no longer working.
include_once("../facebooksdk/src/facebook.php");
$facebook = new Facebook(array(
'appId' => 'XX',
'secret' => 'XX',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = 'XX';
$page_info = $facebook->api("/".$page_id."?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => "Welcome to TuneHub!"
);
$post_id = $facebook->api("/".$page_id."/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
I cant figure out why it would work temporarily, then the following day when the code has been added to the live site, its no longer working (on the test or live site, it isnt working)
is there an API change that is killing the function?
or is there something I am doing wrong with the code that flagged Facebook to prevent it from posting?
(the code may have changed slightly from the script i had which was working, as ive been fiddling around with it to try and find the issue)
see
https://developers.facebook.com/docs/reference/api/page/#posts
Check your access token it may be expired, you need to have a valid access token with rights to post on wall, try getting the updated access token and try

Facebook login/connect help - PHP

I am having problems with Facebook connect.
Once I have the connect with Facebook button how do i retrieve the info with php?
There are loads of examples in developer.facebook.com but I couldn't get any of them to work.
Is it POST or GET or what? (I'm kinda new to php... )
Thanks.
DEMO: http://so.devilmaycode.it/facebook-login-connect-help-php/
Setup new App here
Download the PHP SDK here
Copy & Paste the code you'll find here into a file called index.php
Edit the first lines of the code above and provide the needed informations like below:
require 'sdk/facebook.php'; //the path to the downloaded PHP SDK
$facebook = new Facebook(array(
'appId' => '105810212821284', //App ID you find once created the app
'secret' => '3d6fdaa377cd4ca9...', //Secret Key you find once created the app
'cookie' => true,
));
you have done ;)
Application Settings:
Web Site -> Site URL -> http://so.devilmaycode.it/facebook/
Web Site -> Site Domain -> so.devilmaycode.it
Facebook Integration -> Canvas URL -> http://so.devilmaycode.it/facebook/
Requiriment
PHP5
CURL Lib
Check if you have the requirement
<? echo phpinfo(); ?>
Demo Source
http://pastebin.com/KyQ3CHV0
Use https://github.com/facebook/php-sdk/
Example
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
Display the data
echo $me['email']
http://developers.facebook.com/docs/reference/api/user/
More info about Graph API
To get the email or aditional data from users, you need to request it first(if not you only can get the basic data, like name / picture or only the public data), check the api facebook file in the function called "getLoginUrl".
'api_key' => $this->getAppId(),
'cancel_url' => $currentUrl,
'display' => 'page',
'req_perms' => 'email', <------------- email
'fbconnect' => 1,
'next' => $currentUrl,
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',

Facebook SDK photos returns blank array

I am using this to retrieve user photos however it always print_r's a blank array:
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
'cookie' => true,
));
if ($facebook->getSession()) {
try {
$profile = $facebook->api('/me/photos');
echo "<pre>";
print_r($profile);
} catch (FacebookApiException $e) {
echo $e->getMessage();
exit();
}
}
else {
$login_url = $facebook->getLoginUrl(array(
'next' => 'XXX',
'cancel_url' => 'XXX',
'req_perms' => 'user_about_me,user_location,user_hometown,user_birthday,user_interests,user_likes,user_photos'
));
echo 'Click here to login';
}
If I use:
$profile = $facebook->api('/me');
It print_r's an array full of data but not the photo information I am looking for.
Any ideas?
I was having the same problem. My solution :
$facebook->api('/me/albums?access_token=' . $session['access_token']);
Hope It's not too late !
When you get the facebook session you also receive an access token. You have to use it to make valid api requests. Everything that's not public info require that token. Example of usage:
$session = $facebook->getSession();
// var_dump($session) to see it's content
$facebook->api('/me/photos?access_token=' . $session['access_token']);
If doing this you still get empty data, maybe that user does not have any photos.
Good luck!
Try https://graph.facebook.com/$album_id/photos
I am not sure why me/photos does return empty array. Probably it means something else, may be the photos where I am tagged or some other thing. For me too, it returns empty array consistently. but when I try to access graph.facebook.com/$album_id/photos then I get the array of all the photos in that album..

Categories