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 encodecoordinates
parameter for PHP SDK. place
andcoordinates
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
来源:https://stackoverflow.com/questions/11072933/facebook-publish-checkins-using-php-sdk-javascript-sdk