1

我正在使用 Facebook php sdk v3.2.0,并且在使用 AND 查询(如西瓜+香蕉)搜索帖子时只返回一个空数据集。我目前正在从命令行运行这个脚本,如果这有什么不同的话:

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

4

3 回答 3

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);
    }
}

?>
于 2012-11-27T11:40:56.317 回答
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`
于 2013-11-22T10:49:38.430 回答
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);
  }
}
于 2012-11-27T11:36:14.207 回答