Reputation: 28
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
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
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
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