用PHP发布事件页面 - 正确的页面? [英] Posting an Event to Page Wall with PHP - The Proper Page?
问题描述
我缺少什么?这似乎是所有的,但显然是内向外或向后。
任何和所有的帮助是赞赏!以下是代码:
<?php
$ app_id =xxxxxxxxxxxxxxxxxxxxxxxxxxx;
$ app_secret =xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
$ my_url =http://xxxxxxxxxxxxxxxxxxxxxx.com/testfiles/fbeventform.php;
//要获取PAGE访问代码
//首先获取用户访问代码
session_start();
$ code = $ _REQUEST [code];
if(empty($ code)){
$ _SESSION ['state'] = md5(uniqid(rand(),TRUE)); // CSRF保护
$ dialog_url =https://www.facebook.com/dialog/oauth?client_id=
。 $ app_id。 & redirect_uri =。 urlencode($ my_url)。 & state =
。 $ _SESSION ['state']。 &安培;范围= create_event&安培;范围= manage_pages;
echo(< script> top.location.href ='$ dialog_url。'< / script>);
}
if($ _ REQUEST ['state'] == $ _SESSION ['state']){
$ token_url =https://graph.facebook.com / OAuth的/的access_token?
。 client_id =。 $ app_id。 & redirect_uri =。 urlencode($ my_url)
。 & client_secret =。 $ app_secret。 & code =。 $代码;
$ access_token = @file_get_contents($ token_url);
$ params = null;
parse_str($ access_token,$ params);
$ graph_url =https://graph.facebook.com/me?access_token=
。 $ PARAMS [ ACCESS_TOKEN];
$ user = json_decode(file_get_contents($ graph_url));
echo(Hello。$ user-> name);
}
else {
echo(状态不匹配,您可能是CSRF的受害者);
}
echo'< hr />'。 $的access_token;
//现在,使用用户访问令牌
$ page_token_url =https://graph.facebook.com/获取PAGE访问令牌。 $ page_id。 ?字段=&的access_token放大器; 。 $的access_token;
$ response = file_get_contents($ page_token_url);
//解析返回值并获取页面访问令牌
$ resp_obj = json_decode($ response,true);
$ page_access_token = $ resp_obj ['access_token'];
echo'< hr />'。 $ page_access_token;
//发布事件 - 以下是窗体函数
if(!empty($ _ POST)&&(empty($ _ POST ['name']) || empty($ _ POST ['start_time'])|| empty($ _ POST ['end_time'])){
$ msg =请检查你的输入!
} elseif(!empty($ _ POST)){
$ url =https://graph.facebook.com/。 $ page_id。 /事件? 。 $的access_token;
$ params = array();
//准备事件字段
foreach($ _ POST as $ key => $ value)
if(strlen($ value))
$ params [$ key] = $值;
//检查我们是否有图像
如果(isset($ _ FILES)&&空)($ _ FILES ['picture'] ['name'])){
$ uploaddir ='./upload/';
$ uploadfile = $ uploaddir。基名($ _ FILES [画面] [名称]);
if(move_uploaded_file($ _ FILES ['picture'] ['tmp_name'],$ uploadfile)){
$ params ['picture'] =@真实路径($ uploadfile);
}
}
//启动Graph API调用
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ params);
$ result = curl_exec($ ch);
$ decoded = json_decode($ result,true);
curl_close($ ch);
if(is_array($ decoding)&& isset($ decoding ['id'])){
//事件创建成功,现在我们可以
// a)保存事件id到DB AND / OR
// b)显示成功消息AND / OR
// c)可选地,从我们的服务器删除图像(如果有)
$ msg =事件创建成功:{$ decoded ['id']};
}
}
?>
< form enctype =multipart / form-dataaction =method =post>
< p>< label for =name>事件名称< / label>< input type =textname =namevalue =a/>< / p>
< p>< label for =description>事件描述< / label>< textarea name =description>< / textarea>< / p>
< p>< label for =location>位置< / label>< input type =textname =locationvalue =/>< / p>
< p>< label for =>开始时间< / label>< input type =textname =start_timevalue =<?php echo date('Ymd H: i:s');?> />< / p为H.
< p>< label for =end_time>结束时间< / label>< input type =textname =end_timevalue =<?php echo date('Ymd H :i:s',mktime(0,0,0,date(m),date(d)+ 1,date(Y)));?& />< / p为H.
< p>< label for =picture>事件图片< / label>< input type =filename =picture/>< / p>
< p>
< label for =privacy_type>隐私< / label>
< input type =radioname =privacy_typevalue =OPENchecked ='checked'/>打开& nbsp;& nbsp;& nbsp;
< input type =radioname =privacy_typevalue =已关闭/>已关闭& nbsp;& nbsp;& nbsp;
< input type =radioname =privacy_typevalue =SECRET/> Secret& nbsp;& nbsp;& nbsp;
< / p>
< p>< input type =submitvalue =Create Event/>< / p>
< / form>
< / body>
< / html>
在您的 $ url = https://graph.facebook.com/。 $ page_id。 /事件? 。 $ access_token;
你缺少访问令牌的查询字符串名称
尝试这个
$ url =https://graph.facebook.com/。 $ page_id。 / events?access_token =。 $ access_token;
I'm getting tantalizingly close to completing this script. The goal is to Post an event to one of my fan page walls. The script is effectively grabbing the correct PAGE Access Token, and is effectively posting the event, but it's on my personal wall, not the page wall I'm trying to get it to.
What am I missing? It seems to be all there, but clearly something is inside out or backwards.
Any and all help is appreciated! Here's the code:
<?php
$app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://xxxxxxxxxxxxxxxxxxxxxx.com/testfiles/fbeventform.php";
//Going to get the PAGE access code
//First to get USER Access Code
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'] . "&scope=create_event&scope=manage_pages";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$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);
$params = null;
parse_str($access_token, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
echo '<hr />' . $access_token;
//Now, getting the PAGE Access token, using the user access token
$page_token_url = "https://graph.facebook.com/" . $page_id . "?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
// Parse the return value and get the Page access token
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];
echo '<hr />' . $page_access_token;
//Post the event--here's the form function
if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
$msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
$url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token;
$params = array();
// Prepare Event fields
foreach($_POST as $key=>$value)
if(strlen($value))
$params[$key] = $value;
// Check if we have an image
if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
$uploaddir = './upload/';
$uploadfile = $uploaddir . basename($_FILES['picture']['name']);
if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
$params['picture'] = "@" . realpath($uploadfile);
}
}
// Start the Graph API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
$decoded = json_decode($result, true);
curl_close($ch);
if(is_array($decoded) && isset($decoded['id'])) {
// Event created successfully, now we can
// a) save event id to DB AND/OR
// b) show success message AND/OR
// c) optionally, delete image from our server (if any)
$msg = "Event created successfully: {$decoded['id']}";
}
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p><label for="name">Event Name</label><input type="text" name="name" value="a" /></p>
<p><label for="description">Event Description</label><textarea name="description"></textarea></p>
<p><label for="location">Location</label><input type="text" name="location" value="" /></p>
<p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
<p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))); ?>" /></p>
<p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
<p>
<label for="privacy_type">Privacy</label>
<input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open
<input type="radio" name="privacy_type" value="CLOSED" />Closed
<input type="radio" name="privacy_type" value="SECRET" />Secret
</p>
<p><input type="submit" value="Create Event" /></p>
</form>
</body>
</html>
On your $url = "https://graph.facebook.com/" . $page_id . "/events?" . $access_token;
you're missing the query string name for the access token
Try this
$url = "https://graph.facebook.com/" . $page_id . "/events?access_token=" . $access_token;
这篇关于用PHP发布事件页面 - 正确的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!