yuli chika
yuli chika

Reputation: 9221

Facebook api search.

I'm new to studying the Facebook API search. I use this code, but the resault is empty. Can anyone help me? Thanks.

<form action ="index.php" method ="post">
<input type="text" value="what is?" name="search" style="color:#999;text-align:center;" onfocus="if (value =='what is?'){value =''}" onblur="if (value ==''){value='what is?'}"/>
<input type ="submit" value="ok" />
</form>
<?php
function callFb($url)
{
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true
    ));

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

$url = "https://graph.facebook.com/oauth/access_token?client_id=api_id&redirect_uri=my_url&client_secret=my_secret";
$access_token = callFb($url);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));

$url1 = "https://graph.facebook.com/search?access_token=".$access_token."&q=".urlencode($_POST['search'])."&type=user";
$ret_json = callFb($url1);
$users = json_decode($ret_json, true);

?>

<img src="https://graph.facebook.com/<? echo $users[data][0][id]; ?>/picture?type=small">

Upvotes: 3

Views: 2095

Answers (2)

Apoorv
Apoorv

Reputation: 1379

make sure that you not passing anything as nil or null

try this with you access token in browser

https://graph.facebook.com/search?q=mark&type=user&access_token=<%=your token%>

you will get the following response

{
   "data": [
      {
         "name": "Mark Maglasang",
         "id": "142137849672813"
      },
      {
         "name": "Mark Daniel",
         "id": "137983696724314"
      },
      {
         "name": "Mark Zuckerberg",
         "id": "10101640953722381"
      },
      {
         "name": "Mujeres Del Markham Desnudas",
         "id": "1046548842073860"
      },
      {
         "name": "Mark Tarello",
         "id": "10153122965377699"
      },
      {
         "name": "Mark Feister",
         "id": "128516701078456"
      },
      {
         "name": "Mark Connors",
         "id": "828042037238121"
      },
      {
         "name": "Mark Schonbach",
         "id": "10102125662137944"
      },
      {
         "name": "Gift Mark",
         "id": "276086699535224"
      },

Upvotes: 0

Kyle Wild
Kyle Wild

Reputation: 8915

Your $url parameter is set to:

"https://graph.facebook.com/oauth/access_token?ient_id=api_id&redirect_uri=my_url&client_secret=my_secret";

The values for 'client_id', 'redirect_uri', and 'client_secret' are still set to placeholders. The best way to handle this is like so:

<?

// params to be encoded in the auth URL
$params = array(
    'client_id' => $fb_app_id,
    'redirect_uri' => $my_redirect_url,
    'client_secret' => $fb_app_secret,
);

// encode the params in to a string
$paramString = "?";
foreach($params as $key=>$value) {
    $paramString .= "&{$key]=" . urlencode($value);
}

// auth URL
$url = "https://graph.facebook.com/oauth/access_token" . $paramString;

You should assign the real desired values to $fb_app_id, $my_redirect_url, and $fb_app_secret.

For the $fb_app_id and $fb_app_secret, you can get them from your app's settings in the Facebook Developer area for the app in question (at http://www.facebook.com/developer).

The value of $my_redirect_url has to be a real URL within the 'Web Site' tab in the Edit App area: http://drktd.com/3zc4

Upvotes: 4

Related Questions