Facebook - 使用PHP SDK / JavaScript SDK发布Checkins

时间:2012-06-17 16:27:48

标签: facebook facebook-javascript-sdk facebook-php-sdk checkin

我正在尝试使用Facebook Graph API发布checkin。我已完成Facebook API documentation (checkins)并获得publish_checkins权限。但是,我的签到没有发布。我可以知道有什么不对或我错过了什么吗?谢谢您的时间:))

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 - 使用PHP SDK (此示例中使用的SDK不正确,应使用JavaScript SDK)

<?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
}
?>

问题已解决 - 发布签到时需要注意的事项

  • 确保授予publish_checkins权限。
  • 必须使用json_encode()为PHP SDK编码coordinates参数。
  • placecoordinates参数是强制性的。
  • 如果您刚刚向现有的允许权限列表添加了publish_checkins权限,则需要重新进行身份验证。

2 个答案:

答案 0 :(得分:1)

显然,我在问题中发布的配置和PHP签入功能是正确的。但是,正如Nehal所指出的,我应该使用JavaScript SDK代替PHP SDK。供将来参考......

使用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!");
        }
    );
}

答案 1 :(得分:0)

您还需要学习PHP和变量范围。

$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