如何使用多个单词php-sdk搜索Facebook Graph API

时间:2012-11-27 10:25:36

标签: php facebook facebook-graph-api facebook-php-sdk

我正在使用Facebook php sdk v3.2.0,并且只使用AND查询搜索帖子时返回一个空数据集,例如:watermelon + banana。我目前正在从命令行运行此脚本,如果这有任何区别:

$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 =“西瓜”时,它确实有效。我在不同的机器上试过这个但也没有骰子。有谁知道发生了什么?

3 个答案:

答案 0 :(得分: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);
    }
}

?>

答案 1 :(得分: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`

答案 2 :(得分: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);
  }
}