2010-12-20 149 views
3

我是学习Facebook API搜索的新手。 我使用这段代码,但是resault是空的。谁能帮我?谢谢。Facebook api搜索。

<form action ="index.php" method ="post"> 
<input type="text" value="what is?" name="search" style="color:#999;text-align:center;" onfocus="if (value =='what is?'){value =''}" onblur="if (value ==''){value='what is?'}"/> 
<input type ="submit" value="ok" /> 
</form> 
<?php 
function callFb($url) 
{ 
    $ch = curl_init(); 
    curl_setopt_array($ch, array(
     CURLOPT_URL => $url, 
     CURLOPT_RETURNTRANSFER => true 
    )); 

    $result = curl_exec($ch); 
    curl_close($ch); 
    return $result; 
} 

$url = "https://graph.facebook.com/oauth/access_token?client_id=api_id&redirect_uri=my_url&client_secret=my_secret"; 
$access_token = callFb($url); 
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token)); 

$url1 = "https://graph.facebook.com/search?access_token=".$access_token."&q=".urlencode($_POST['search'])."&type=user"; 
$ret_json = callFb($url1); 
$users = json_decode($ret_json, true); 

?> 

<img src="https://graph.facebook.com/<? echo $users[data][0][id]; ?>/picture?type=small"> 
+0

'https://graph.facebook.com/oauth/access_token?client_id = api_id&redirect_uri = my_url&client_secret = my_secret' 您需要首先用适当的数据替换'api_id','my_url'和'my_secret' – 2010-12-20 14:28:08

+0

@Horia Dragomir,yes ,我已经取代了我的,但结果是空的,即使我搜索我自己的名字。 – 2010-12-20 14:48:53

+0

'$ access_token'是否意味着有效? – ajreal 2010-12-20 14:59:39

回答

4

你的$ url参数设置为:

“https://graph.facebook.com/oauth/access_token?ient_id=api_id & REDIRECT_URI = my_url & client_secret = my_secret”;

'client_id','redirect_uri'和'client_secret'的值仍设置为占位符。处理这种情况最好的办法是,像这样:

<? 

// params to be encoded in the auth URL 
$params = array(
    'client_id' => $fb_app_id, 
    'redirect_uri' => $my_redirect_url, 
    'client_secret' => $fb_app_secret, 
); 

// encode the params in to a string 
$paramString = "?"; 
foreach($params as $key=>$value) { 
    $paramString .= "&{$key]=" . urlencode($value); 
} 

// auth URL 
$url = "https://graph.facebook.com/oauth/access_token" . $paramString; 

你应该分配真正需要的值$fb_app_id$my_redirect_url,和$ fb_app_secret。

对于$fb_app_id$fb_app_secret,您可以从您应用的相关应用(位于http://www.facebook.com/developer)的Facebook Developer区域的设置中获取它们。

$my_redirect_url的值必须是“网站”的编辑APP区域选项卡内的真实网址:http://drktd.com/3zc4

0

确保你不传递任何作为零或零

尝试这与您访问令牌在浏览器

https://graph.facebook.com/search?q=mark&type=user&access_token=<%=your token%> 

您将得到以下响应

{ 
    "data": [ 
     { 
     "name": "Mark Maglasang", 
     "id": "142137849672813" 
     }, 
     { 
     "name": "Mark Daniel", 
     "id": "137983696724314" 
     }, 
     { 
     "name": "Mark Zuckerberg", 
     "id": "10101640953722381" 
     }, 
     { 
     "name": "Mujeres Del Markham Desnudas", 
     "id": "1046548842073860" 
     }, 
     { 
     "name": "Mark Tarello", 
     "id": "10153122965377699" 
     }, 
     { 
     "name": "Mark Feister", 
     "id": "128516701078456" 
     }, 
     { 
     "name": "Mark Connors", 
     "id": "828042037238121" 
     }, 
     { 
     "name": "Mark Schonbach", 
     "id": "10102125662137944" 
     }, 
     { 
     "name": "Gift Mark", 
     "id": "276086699535224" 
     },