1

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.
Mysophobe
  • 622
  • 2
  • 10
  • 33
  • What error are you getting? Are you getting a post ID back? if so, is that appearing in the user's activity log? – Igy Jun 17 '12 at 16:36
  • @Igy Unfortunately, I'm not getting any error from php_error.log. I've added try catch inside the function but I'm also not getting any exception. However something strange caught my attention, when I did an 'alert($facebook->getAccessToken()?>);' it prompted "undefined". May I know how do I check for a post ID back and what is the user's activity log? I've just started developing Facebook application a month ago, would really appreciate it if you would guide me. Thank you :) – Mysophobe Jun 18 '12 at 12:33
  • When you make that API call there should be a callback with an error or the post ID of the new post you've created – Igy Jun 18 '12 at 12:52
  • Did you globalize $uid and $facebook for use inside the checkin() function? – Don Wilson Jun 18 '12 at 13:36
  • @Igy I referred to [facebook-api-callback-function](http://stackoverflow.com/questions/6408814/facebook-api-callback-function) and did an alert () at the end of both try and catch clause but didn't get any prompt from them. Also, any alert() in the function will not work when I make the API call. – Mysophobe Jun 18 '12 at 14:17
  • @DonWilson I didn't globalize them but it prompted me my uid which is "564668616" when I did an alert(=$uid?>). – Mysophobe Jun 18 '12 at 14:21

2 Answers2

1

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!");
        }
    );
}
Mysophobe
  • 622
  • 2
  • 10
  • 33
  • 1
    Please be aware that according to the recent [Facebook docs](https://developers.facebook.com/docs/reference/api/checkin/) _"Publishing a Checkin object is deprecated in favor of creating a Post with a location attached."_ – JimmyBlu Oct 22 '13 at 15:42
0

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
Nehal
  • 1,004
  • 1
  • 10
  • 33
  • I followed and did a '' instead to call the function and it returned me an exception "class Facebook could not be converted to string". – Mysophobe Jun 18 '12 at 14:49
  • 1
    It's a PHP function, you can't use it in javascript call. To use facebook API for javascript try [Facebook Javascript SDK](https://developers.facebook.com/docs/reference/javascript/) – Nehal Jun 19 '12 at 05:41