naschans
naschans

Reputation: 28

How to search Facebook Graph API with multiple words php-sdk

I am using the Facebook php sdk v3.2.0, and am only getting an empty data set returned when searching for posts using an AND query like: watermelon+banana. I am currently running this script from the commandline, if that makes any difference:

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

when just going to http://graph.facebook.com/search?q=watermelon+banana&type=post in my browser, I can see the results. Also, when querying $q="watermelon" it does work. I've tried this on different machines but also no dice. Does anyone know what's going on?

Upvotes: 1

Views: 9092

Answers (3)

Tabish
Tabish

Reputation: 1712

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`

Upvotes: 1

phwd
phwd

Reputation: 19995

You are encoding + when you don't need to do so.

So your query in PHP is actually http://graph.facebook.com/search?q=watermelon%2Bbanana&type=post&limit=10

leave out the urlencode function

$q = "watermelon+banana" ;

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

So the full code looks like

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

?>

Upvotes: 1

Viren Rajput
Viren Rajput

Reputation: 5746

You're code should just work fine now (have made all the necessary changes):

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

Upvotes: 0

Related Questions