在过去的几天里,我正在研究需要在Facebook上搜索用户的应用程序。自"username" was deprecated/canceled的FQL查询以来,我决定使用通用搜索API。
我使用PHP,所以FB PHP SDK是我喜欢的方式。我之前已经将它用于FQL查询,就像这样:
// $api is already initialized, with access_key, app secret and so on
$users = $api(array(
'method' => 'fql.query',
'query' => "SELECT first_name,last_name FROM user WHERE uid='12345'",
));
我想以类似的方式构建搜索查询。特别是,我不想对参数进行urlencode,指定访问密钥,app秘密以及SDK应该为我做的所有事情。但是,我还没能使用SDK构建此查询。有没有可能做到这一点?如果有,怎么样?我找到了sdk支持的“api调用”的长列表,但我需要为graph.facebook.com/search?arguments构建查询。
提前致谢。
编辑:为了说清楚,我不想自己构建字符串。我知道这个解决方案有效。但是当我拥有SDK时,它很丑陋:
$name = urlencode(trim($first_name . " " . $last_name_));
$users = $this->facebook->api("/search?q=$name&type=user&access_token=$key");
答案 0 :(得分:0)
使用php-sdk 3.1.1
通过Graph API搜索用户{
"error": {
"message": "A user access token is required to request this resource.",
"type": "OAuthException"
}
}
<?php
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'your-app-id',
'secret' => 'your-app-secret',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
/* */
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<?php
/* Get Search parameter from url or post and urlencode it. */
$q = urlencode($_GET['qs']);
if(!$_GET['qs']){
$q = urlencode($_POST['qs']);
if(!$_POST['qs']){
/* Default Search Term */
$q = "Shawn+E+Carter";
}
}
/* Get Results Limit from url or set default. */
$limit = $_GET['limit'];
if (!$_GET['limit']){
$limit = 60;
}
/* Get Offset from url or set default for paging. */
$offset = $_GET['offset'];
if (!$_GET['offset']){
$offset = 0;
}
/* Make Graph API call to user */
$usersearch = 'search%3Fq='.$q.'%26type=user%26limit='.$limit.'%26offset='.$offset.'';
echo '<pre style="text-align: left;">';
print_r($usersearch);
echo '</pre>';
?>