2012-11-27 53 views
1

我正在使用Facebook的php sdk v3.2.0,並且在搜索帖子時使用AND查詢返回空數據集:西瓜+香蕉。我目前運行在命令行這個腳本,如果有什麼差別:如何在多個單詞中搜索Facebook Graph API php-sdk

$facebook = new Facebook(array(
'appId' => 'MY_APP_ID', 
'secret' => 'MY_SECRET', 
)); 

$q = "watermelon+banana" ; 

$search = $facebook->api('/search?q='.$q.'&type=post&limit=10'); 

foreach ($search as $key=>$value) { 
    foreach ($value as $fkey=>$fvalue) { 
    print_r ($fvalue); 
} 
} 

時只是將http://graph.facebook.com/search?q=watermelon+banana&type=post在我的瀏覽器,我可以看到結果。而且,當查詢$ q =「西瓜」時,它確實有效。我在不同的機器上試過這個,但也沒有骰子。有誰知道發生了什麼事?

回答

1

您編碼+當你不需要這麼做。

所以你在PHP查詢實際上是http://graph.facebook.com/search?q=watermelon%2Bbanana&type=post&limit=10

離開了urlencode功能

$q = "watermelon+banana" ; 

$search = $facebook->api('/search?q='.$q.'&type=post&limit=10'); 

所以完整的代碼看起來像

<?php 

require 'facebook.php'; 

$facebook = new Facebook(array(
    'appId' => 'YOUR_APP_ID', 
    'secret' => 'YOUR_SECRET', 
)); 

$q = "watermelon+banana" ; 

$search = $facebook->api('/search?q='.$q.'&type=post&limit=10'); 

foreach ($search as $key=>$value) { 
    foreach ($value as $fkey=>$fvalue) { 
     print_r ($fvalue); 
    } 
} 

?> 
+0

感謝您的回覆,不幸的是我仍然得到相同的結果:一個空的數據對象。當只用「西瓜」這樣的單詞來表達你的答案時,它就可以很好地工作。有可能是我的PHP配置有問題? – naschans

+0

@naschans最有可能的,上面的作品適合我。 – phwd

+0

我在不同的機器上嘗試過,但仍然沒有任何結果。有什麼配置的SDK我必須調整? – naschans

0

你的代碼應該只是正常工作,現在(已經做了一切必要的修改):

$facebook = new Facebook(array(
    'appId' => 'xxxxx', 
    'secret' => 'xxxxx', 
)); 

$q = "watermelon banana"; // dont need to urlencode the string 

$q = str_replace(" ","+",$q); // this will replace all occurances of spaces with + 

$search = $facebook->api('/search?q='.$q.'&type=post&limit=10'); 

foreach ($search as $key=>$value) { 
    foreach ($value as $fkey=>$fvalue) { 
    print_r ($fvalue); 
    } 
} 
+0

我想這一點,但它仍然給我一個空對象{「data」:[]} ...當我用「西瓜」嘗試它時,它工作得很好howeve河 – naschans

1
require '../src/facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$config = array(
    'appId' => 'xxxxxxxxxxxx', 
    'secret' => 'xxxxxxxxxxxxxxxxxx', 
    'allowSignedRequest' => false // opt`enter code here`ional but should be set to false for non-canvas apps 
); 
    $facebook = new Facebook($config); 
    $user_id = $facebook->getUser(); 

    $query = urlencode('india+China'); 
$type = 'post'; 
$retrive = $facebook->api('/search?q='.$query.'&type='.$type.'&limit=10'); 

$string= json_encode($retrive); 
$json_a = json_decode($string, true); 
$json_o = json_decode($string); 

foreach($json_o->data as $p) 
{ 
$text = $p->message; 
     $username=$p->from->name; 
     $id=$p->from->id; 
     echo "<table border=1px> 
<tr> 
<td> 
<td>$id</td> 
<td>$username</td> 
<td>$text</td> 
</tr> 
</table>"; 

}`enter code here`