我的问题很简单,我想向api发送关键字以搜索页面名称中具有相同关键字的页面,并且我想返回所有包含详细信息的页面,例如每个页面及其喜欢的数目和关于部分等。 ...
我该怎么做,我下载了Facebook php sdk,并按照文档操作,并使用以下代码。
我在Facebook开发者帐户上制作了一个应用,并且使用了该应用的ID和密码(该应用是实时的);
<?php
require_once('facebook/autoload.php'); // change path as needed
$fb = new \Facebook\Facebook([
'app_id' => '*****',
'app_secret' => '******',
'default_graph_version' => 'v2.10',
//'default_access_token' => '{access-token}', // optional
]);
$helper = $fb->getRedirectLoginHelper();
// app directory could be anything but website URL must match the URL
given in the developers.facebook.com/apps
define('APP_URL', 'http://kout-kw.com/auto_search/testf.php');
$permissions = []; // optional
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// redirect the user back to the same page if it has "code" GET variable
if (isset($_GET['code'])) {
header('Location: ./');
}
// type can be user, group, page or event
$search = $fb->get('/search?q=programming&type=page');
$search = $search->getGraphEdge()->asArray();
foreach ($search as $key) {
echo $key['name'] . '<br>';
}
// Now you can redirect to another page and use the access token from
$_SESSION['facebook_access_token']
} else {
// replace your website URL same as added in the
//developers.facebook.com/apps e.g. if you used http instead of https and
//you used non-www version or www version of your website then you must
//add
// the same here
$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}
当我运行代码时,出现一个带有文本的页面。使用Facebook登录! 我单击它,然后出现以下错误
无法加载URL:此URL的域未包含在应用程序的域中。为了能够加载此URL,请将应用程序的所有域和子域添加到应用程序设置的“应用程序域”字段中。
我解决了此错误,原因是我应该将redirectit URL放在Facebook登录设置中,但是现在我遇到了新问题
Facebook SDK返回错误:跨站点请求伪造验证失败。 URL和会话的“状态”参数不匹配。
有什么想法吗?
更新 我通过添加
解决了过去的错误$_SESSION['FBRLH_state']=$_GET['state'];
现在的问题是我没有错误,但是以下代码没有给我任何输出?
有人知道为什么吗?
// type can be user, group, page or event
$search = $fb->get('/search?q=programming&type=group');
$search = $search->getGraphEdge()->asArray();
foreach ($search as $key) {
echo $key['name'] . '<br>';
}