yuli chika yuli chika - 3 years ago 75
PHP Question

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">

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download