I'm trying to publish checkin using Facebook Graph API. I've gone through Facebook API documentation (checkins) and also have the publish_checkins permission. However, my checkin is not getting published. May I know is there anything wrong or am I missing anything else? Thank you for your time :)
fbmain.php
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
// Session based API call
if ($user) {
try {
$me = $facebook->api('/me');
if($me)
{
$_SESSION['fbID'] = $me['id'];
$uid = $me['id'];
}
} catch (FacebookApiException $e) {
error_log($e);
}
}
else {
echo "<script type='text/javascript'>top.location.href='$loginUrl';</script>";
exit;
}
$loginUrl = $facebook->getLoginUrl(
array(
'redirect_uri' => $redirect_url,
'scope' => status_update, publish_stream, publish_checkins,
user_checkins, user_location, user_status'
)
);
main.php - Using PHP SDK (Wrong SDK used in this example, should use JavaScript SDK instead)
<?php
include_once "fbmain.php";
if (!isset($_POST['latitude']) && !isset($_POST['longitude']))
{
?>
<html>
<head>
//ajax POST of latitude and longitude
</head>
<body>
<input type="button" value="Check In!" onclick="checkin();"/></span>
</body>
</html>
<?php
}
else
{
?>
<script type="text/javascript">
function checkin()
{
try
{
$tryCatch = $facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '165122993538708',
'message' =>'MESSAGE_HERE',
'coordinates' => json_encode(array(
'latitude' => '1.3019399200902',
'longitude' => '103.84067653695'
))
));
}
catch(FacebookApiException $e)
{
$tryCatch=$e->getMessage();
}
return $tryCatch;
}
</script>
<?php
}
?>
Question solved - Things to take note when publishing checkin
Make sure publish_checkins permission is granted.
Must use json_encode() to encode coordinates parameter for PHP SDK.
place and coordinates parameters are compulsory.
A re-authentication is required if you have just added publish_checkins permission to your existing list of allowed permissions.
Apparently, the configuration and the PHP checkin function that I have posted in the question are correct. However, I should use JavaScript SDK instead of PHP SDK for my case as pointed out by Nehal. For future references...
Using JavaScript SDK
function checkin()
{
FB.api('/me/checkins', 'post',
{ message: 'MESSAGE_HERE',
place: 165122993538708,
coordinates: {
'latitude': 1.3019399200902,
'longitude': 103.84067653695
}
},
function (response) {
alert("Checked in!");
}
);
}
You also need to learn PHP and variable scoping.
$facebook = new Facebook(array(
'appId' => FB_API_KEY,
'secret' => FB_SECRET_KEY,
'cookie' => true,
));
$loginUrl = $facebook->getLoginUrl(
array('scope' => 'status_update,publish_stream,publish_checkins,user_checkins,user_location,user_status,user_checkins')
);
// Session based API call
if ($user) {
try {
$me = $facebook->api('/me');
if($me)
{
$_SESSION['fbID'] = $me['id'];
}
} catch (FacebookApiException $e) {
error_log($e);
}
}
else {
echo "<script type='text/javascript'>top.location.href='$loginUrl';</script>";
exit;
}
function checkin($fb)
{
try
{
$tryCatch = $fb->api('/'.$_SESSION['fbID'].'/checkins', 'POST', array(
'access_token' => $fb->getAccessToken(), //corrected
'place' => '165122993538708',
'message' =>'I went to placename today',
'coordinates' => json_encode(array(
'latitude' => '1.3019399200902',
'longitude' => '103.84067653695'
))
));
}
catch(FacebookApiException $e)
{
$tryCatch=$e->getMessage();
}
return $tryCatch;
}
checkin($facebook); //calling the function and passing facebook object to function
Related
I've been trying all day to figure this out, but apparently it does not work for me.
What i'm looking into doing is ... ask for facebook login, and then add that user to a group ( send an invite ). I've tried to follow the API docs here:
https://developers.facebook.com/docs/reference/api/group/#invite_member
No luck.
My code looks like:
<?php
require "fb/facebook.php";
$access_token = "app_token_here";
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secretBlabla',
'cookie' => true,
'allowSignedRequest' => false,
));
$params = array('scope' => 'read_friendlists,read_insights,user_about_me,user_birthday,user_groups,user_interests,user_likes,user_location,user_relationships,user_website');
$fuser = $facebook->getUser();
if ($fuser) {
try {
$user_profile = $facebook->api("/me");
} catch (FacebookApiException $e) {
error_log($e);
$fuser = null;
}
}
if ($fuser) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($params);
}
$page = "/XXXXXX/members/".$fuser;
$token=$facebook->getAccessToken();
$facebook->api($page, "POST", array("access_token"=>$access_token)); // i've tried with $token as well
Error i get:
PHP Fatal error: Uncaught OAuthException: (#3) Unknown method\n
Any idea what i'm doing wrong ?
Thank you
I have problem with Facebook PHP SDK. It always throws that exception. I tried many solutions listed here, but nothing works for me.
It seems that Facebook returns to me valid access token, because I tested it with Debug tool in dashboard of my application.
What's my scenario?
I want to post to publish simple content to user's wall by calling static function:
function social_publish($network, $title, $message, $link = '', $image = '') {
global $_config;
// Initialize Facebook SDK
$facebook = new Facebook(array(
'appId' => $_config['fb_app']['app_id'],
'secret' => $_config['fb_app']['app_security_key']
));
// Set data
$attachment = array(
'name' => $title,
'caption' => $title,
'message' => $message,
'link' => $link,
'picture' => $image,
'actions' => array('name' => 'Test', 'link' => 'Link')
);
try {
$access_token = $facebook->getAccessToken(); // returns valid access token
$uid = $facebook->getUser(); // always return 0
$result = $facebook->api( '/' . $_config['fb_profile'] . '/feed/', 'post', $attachment); // $_config['fb_profile'] procudes 'me' in this case
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
Just to note: I am not working on local environment.
problem solve it as asked in scope request to Facebook for authentication, as I decided to use the JavaScript SDK-and then, here's the solution:
FB.getLoginStatus(function(response) {
if ( ! response.authResponse ) {
FB.login(function(response) {
// handle the response if needed
}, {scope: 'publish_actions, publish_stream'});
}
});
Thank you! :-)
Hi Guys i'm e newbie in php programin and facebook app, i found this code and it works very well for TEXT posts, but my requirements are that I have to UPLOAD a picture too, i tried by addin ['source' => '$image_source'] on the arrey but does not work...
How can I upload a PHOTO ?
<?php
include_once("config.php");
if($_POST)
{
//Post variables we received from user
$userPageId = $_POST["userpages"];
$userMessage = $_POST["message"];
$image_source = "http://www.example.com/image.jpg"
if(strlen($userMessage)<1)
{
//message is empty
$userMessage = 'No message was entered!';
}
//HTTP POST request to PAGE_ID/feed with the publish_stream
$post_url = '/'.$userPageId.'/feed';
//posts message on page statues
$msg_body = array(
'message' => $userMessage,
'source' => '$image_source'
);
if ($fbuser) {
try {
$postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}else{
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
header('Location: ' . $loginUrl);
}
//Show sucess message
if($postResult)
{
echo '<html><head><title>Message Posted</title><link href="style.css" rel="stylesheet" type="text/css" /></head><body>';
echo '<div id="fbpageform" class="pageform" align="center">';
echo '<h1>Your message is posted on your facebook wall.</h1>';
echo '<a class="button" href="'.$homeurl.'">Back to Main Page</a> <a target="_blank" class="button" href="http://www.facebook.com/'.$userPageId.'">Visit Your Page</a>';
echo '</div>';
echo '</body></html>';
}
}
?>
You cannot use the feed endpoint to post photos. You should be using /me/photos with the publish_stream permission. To post to an album you need the /ALBUM_ID/photos endpoint
$msg_body = array(
'message' => $userMessage,
'source' => '#'.'$image_source'
);
$postResult = $facebook->api('/me/photos/','post', $msg_body);
For using urls not associated with your site you need to use url
$msg_body = array(
'message' => $userMessage,
'url' => 'http://somepage.com/img.png'
);
I am facing an issue based on a facebook app script. It always returning 0, even if the user is login to facebook. I have take a look at the old discussions , but I could not find out the solution . Please take a look at the code.
<?php
require "config/database.class.php";
require 'fbook/src/facebook.php';
require "core/corefunction.php";
$facebook = new Facebook(array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
echo "Yes";exit;
}
else{
$urlf= $facebook->getLoginUrl(array('scope' => 'user_status,publish_stream'));
?>
<script language="javascript" type="text/javascript">
document.location.href="<?php echo $urlf; ?>";
</script>
<?php
exit;
}
?>
I am getting an url like this after authenticating facebook,
http://www.example.com/fblog.php?state=91e3e7013fcfeb01df23eed352add987&code=AQAyitCNGSqtCEwMueFi5RvFILI0YC2P4IFdaY0TFK7_ay5vSU1RZh1Ab56DS6aUokQj9VS6RCoYTKmFa0H0AbUFsZBWZTLlFvWqVoOMpwZQcTeCoHcZ1o3NE2VljyoXgfJERreb2ZvL1MhwZ2rKSaInk9SMiyiaNrRXU86fkiL6GBbhrM1aCfuWPq7jqwqG_IM#_=_
it is called continuously ...
I just started making an application for Facebook, however I ran into problem early on. The first step I want people to do is to give permission to access their profile. All over the web are examples of how to do this with:
$user_id = $facebook->require_login();
However, this is the way it works using the Old PHP API. I have downloaded and installed the new one in my application folder and it is not working anymore.
My question is (and i really have been searching for an answer for a long time) what is the code to do this with the new API?
(and related question: is it better to use the old API, or learn to work with the new one when I am just starting making apps right now)
I have this code now;
<?php
// Awesome Facebook Application
//
// Name: -
//
require_once 'facebook-php-sdk/src/facebook.php';
$app_id = "-";
$app_secret = "-";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday, user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
echo "<p>hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";
?>
Download and use new code from github.
How to get user permission?
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,publish_stream,sms,status_update,user_location'
));
Or
<fb:login-button perms="email,user_birthday,publish_stream,sms,status_update,user_location"></fb:login-button>
Example how to login
Update
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday, user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}