我在facebook上有一个活动,我想rsvp参加。我想用链接rsvp,我找到的所有rsvping示例都是用表单完成的。我有rsvp_event权限和活动的access_token。我面临的实际问题是在成功的http帖子后重定向。
<?php
$redirect_uri = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$access_token = "ACCESS_TOKEN";
$eid = "EVENT_ID";
$rsvp_to_event = "https://graph.facebook.com/$eid/attending?method=post&access_token=$access_token&redirect_uri=$redirect_uri";
?>
<a href="<?php echo $rsvp_to_event; ?>">Join</a>
单击“加入”链接将rsvp到该事件,但让我在显示为true的页面上。在成功的rsvp之后有什么想法让页面重定向?
谢谢。
答案 0 :(得分:2)
您必须使用cURL或file_get_contents()
查询该网址,以下是我tutorial的示例:
<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URI";
$event_id = "EVENT_ID";
$rsvp_status = "";
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=rsvp_event";
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
if( isset($_POST['rsvp']) ) {
// Form submitted, call the Graph API to RSVP to the event
$event_rsvp = "https://graph.facebook.com/$event_id/{$_POST['rsvp']}?method=post&" . $access_token;
$rsvped = json_decode(file_get_contents($event_rsvp));
if($rsvped) {
$msg = "Your RSVP status is now <strong>{$_POST['rsvp']}</strong>";
$rsvp_status = $_POST['rsvp'];
}
}
if( !$rsvp_status ) {
$query = "SELECT rsvp_status FROM event_member WHERE eid=$event_id AND uid=me()";
$fql_url = "https://api.facebook.com/method/fql.query?"
. "query=" . urlencode($query)
. "&format=json"
. "&" . $access_token;
$fql_resp = json_decode(file_get_contents($fql_url));
$rsvp_status = $fql_resp[0]->rsvp_status;
}
?>
<!doctype html>
<html>
<head>
<title>Create An Event</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
</style>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form action="" method="post">
<p>
<label for="privacy_type">RSVP:</label>
<input type="radio" name="rsvp" value="attending" <?php if($rsvp_status==="attending") echo "checked='checked'"; ?>/>Attending
<input type="radio" name="rsvp" value="maybe" <?php if($rsvp_status==="maybe" || $rsvp_status==="unsure") echo "checked='checked'"; ?>/>Maybe
<input type="radio" name="rsvp" value="declined" <?php if($rsvp_status==="declined") echo "checked='checked'"; ?>/>Not Attending
</p>
<p><input type="submit" value="RSVP to this event" /></p>
</form>
</body>
</html>
你应该看看:
if( isset($_POST['rsvp']) ) {
// Form submitted, call the Graph API to RSVP to the event
$event_rsvp = "https://graph.facebook.com/$event_id/{$_POST['rsvp']}?method=post&" . $access_token;
$rsvped = json_decode(file_get_contents($event_rsvp));
if($rsvped) {
$msg = "Your RSVP status is now <strong>{$_POST['rsvp']}</strong>";
$rsvp_status = $_POST['rsvp'];
}
}
上面的教程由HTML表单组成,用户可以在其中选择自己的状态。
答案 1 :(得分:0)
大多数图形api端点不是设计为直接使用用户的浏览器调用,而是从后端代码调用它们(与身份验证相关的可能是例外)。
尝试使用offical php sdk从您的php页面发送这些请求,或者只是在您的php脚本中使用这些端点上的curl extension或file_get_contents
并自行重定向用户。