views:

43

answers:

2

Hi there,

I know that this question could have been asked a million times before, but unfortunately I can't compile all these answers to get it working.

I want to write a PHP code that uses facebook graph API, to search for a profile (by name for example) and then grab this information to a local store. I revised facebook graph API but it makes use of OAuth and plenty of overhead tasks that I don't need. Cause simply I don't want be using the API on behalf of specific user account. I just want to use it anonymously, or even with a sample user who shouldn't necessarily be a friend with these users that I'm searching for. I'd be grateful if somebody could tell me whether it is doable or not, or even ask me to clarify my question a little bit more :)

Thanks in advance.

+1  A: 

You have to retrieve an access token to use the Graph API to search for users. I don't think there is a way around it. At least not with the Graph API.

Try here to use the Graph API to search for users. You'll see that they're using an access token. Trying to search for a user without an access token will lead to the following error message.

{
    "error": {
    "type": "OAuthException",
    "message": "An access token is required to request this resource."
    }
}

Update:

From the Facebook Developers documentation:

"Facebook authentication enables your application to interact with the Graph API on behalf of Facebook users, and it provides a powerful single-sign on mechanism across Web, mobile, and desktop apps."

The authentication process isn't that complicated.

Fair Dinkum Thinkum
Thanks for your comment :). can I understand like there should be a logged in user open session on the browser?
rgeorgy
I'm pretty sure you have to be logged in to Facebook. At least I don't know another way to get an access token.
Fair Dinkum Thinkum
+1  A: 

Sure here's a fast way to go about getting the information you want:

<?PHP

define('FACEBOOK_APP_ID', 'yourappIDhere');
define('FACEBOOK_SECRET', 'yourFBsecretkeyhere');

function get_facebook_cookie($app_id, $application_secret) {
    $args = array();
    if(isset($_COOKIE['fbs_' . $app_id])){
        parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    }

    ksort($args);
    $payload = '';
    foreach ($args as $key => $value) {
        if ($key != 'sig') {
            $payload .= $key . '=' . $value;
        }
    }
    if(isset($args['sig'])){
        if (md5($payload . $application_secret) != $args['sig']) {
            return null;
        }
        else
            return $args;
    }
    else
        return null;
}

$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

?>


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:fb="http://www.facebook.com/2008/fbml"&gt;
<head>
</head>
<body>
<?php if ($cookie) { ?>
  Your user ID is <?PHP echo $cookie['uid']; ?>
<?PHP 
    $user = json_decode(file_get_contents('https://graph.facebook.com/me/access_token=' . $cookie['access_token']))->data;

} else { 
?>
  <fb:login-button perms="email"></fb:login-button>
<?php } ?>

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"&gt;&lt;/script&gt;
<script>
  FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
           cookie: true, xfbml: true});
  FB.Event.subscribe('auth.login', function(response) {
    window.location.reload();
  });
</script>
</body>
</html>

After what Fair mentioned above, here's the very BASIC code you need to get the authentication setup for yourself.

HTH :)

You can change the parameters for the login button so it's not just "email" and then search by whatever else you may desire.

ninumedia
Actually, accessing facebook will be in the back-end, I don't need any login buttons for users lo login to facebook. I just want to grab *public* information on user's account. Thanks for your resonse though :)
rgeorgy