如何从Facebook页面提取事件 [英] howto extract events from facebook page
本文介绍了如何从Facebook页面提取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从Facebook页面提取事件到官方网站,并努力与图表和oauth来获取信息。
I want to extract events from a facebook Page to an official website and struggle with graph and oauth to get out the information.
相同代码中的简化代码,阶段1和2:
Simplified code, phase 1&2 in same code:
<?php
$app_id = '...........4639';
$app_secret = '7547xxxxxxxxxxxxxxxxxxxxxxxx';
if (isset($_GET['code'])) {
//stage 2
$state = $_GET['state'];
$code = $_GET['code'];
//the code doesn't work. If I use graph api explorer to get key, I'm all fine.
//the code here from graph api explorer.
//$code = 'AAAIERWeZCHZxxxxxxxxxxxxxxxxxxxxx';
$s = "https://graph.facebook.com/_PAGE_ID?fields=feed.fields(story,message,picture)&access_token=" . $code;
$json = file_get_contents($s);
die($json);
} else {
//stage 1
$my_url = 'http://www.myself.com/fb/index.php';
$_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'];
//now redirect to myself - phase2
header("Location: " . $dialog_url);
exit();
}
推荐答案
如果有人坐在相同的烦恼:
In case anyone sits in the same annoyance:
file_get_contents似乎没有处理令牌请求。以下代码工作。
file_get_contents doesn't seem to cope with the token request. The following code works.
<?php
function curlRequest($url) {
$ch = curl_init();
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$timeout = 5;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$app_id = '................';
$app_secret = '..................................';
$my_url = 'http://www.memyself.andi/me.php';
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
// Redirect to Login Dialog
$_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="
. urlencode($_SESSION['state']) . "&scope=user_birthday,read_stream";
header("Location: " . $dialog_url);
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id=' .
$app_id . '&redirect_uri=' . urlencode($my_url) .
'&client_secret=' . $app_secret . '&code=' . urlencode($code);
//die($token_url);
$response = curlRequest($token_url);
//bummer - this won't work (php 5.3/ubuntu)
//$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$_SESSION['access_token'] = $params['access_token'];
$graph_url = "https://graph.facebook.com/_THE_PAGE_ID/feed?access_token="
. $params['access_token'];
$json = json_decode(file_get_contents($graph_url));
echo serialize($json);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
这篇关于如何从Facebook页面提取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文