Facebook - Publish Checkins using PHP SDK/JavaScript SDK

拈花ヽ惹草 提交于 2019-12-01 12:09:49

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!