1

I'm trying to get some basic information about a user in a PHP script (id and name).

I have tried the following methods:

$retrieve = curl_init("https://graph.facebook.com/me?access_token=$accesstoken");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($retrieve, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($retrieve);
curl_close($retrieve);

and

$user = json_decode(file_get_contents(
         "https://graph.facebook.com/me?access_token=$accesstoken"))->me;

The cURL method (former) just times out. The file_get_contents (latter) method just doesn't return anything at all...

What could be causing this? Am I using cURL correctly?

charles_har
  • 275
  • 1
  • 4
  • 6
  • did you check what the file_get_contents() returns without the json_decode() call and the -> me operator? – Eran Galperin Oct 23 '10 at 23:10
  • Why wouldn't you be using the Facebook PHP SDK to do this? http://github.com/facebook/php-sdk – Nate Totten Oct 23 '10 at 23:10
  • @Eran: Yes, it doesn't return anything at all. It's totally empty. @Nathan: I'm not using the SDK right now, I need to do this manually first. I don't see why this isn't working... – charles_har Oct 23 '10 at 23:18
  • Are you using Linux or IIS? there is a known problem with IIS and https inside file_get_contents() – Eran Galperin Oct 24 '10 at 00:05

2 Answers2

0

for graph api you can use graph api methods rahter than curl the following code grabs information of current user

define('FACEBOOK_APP_ID', 'Your API ID');
define('FACEBOOK_SECRET', 'YOUR SECRET');
function get_facebook_cookie($app_id, $application_secret)
{
    $args = array();
    parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    ksort($args);
    $payload = '';
    foreach ($args as $key => $value)
    {
        if ($key != 'sig')
        {
            $payload .= $key . '=' . $value;
        }
    }
    if (md5($payload . $application_secret) != $args['sig'])
    {
        return null;
    }
    return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
$user=json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']));

its prettey easy

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
-1

Facebook will not let you use curl. They have the api for that.

copy your link and paste it to browser. It will work. In Mozilla you will see the result in browser, IE will save the result as a file. So it is not about invalid access token etc. It is just because Facebook does not respond to your query when it does not come 1-from a web browser, 2-from Facebook APIs.

here is the relevant PHP call to Facebook.

$attachment =  array('access_token' => $access_token);
$result=$facebook->api('/me', 'GET', $attachment);
$id = $result['id'];
$name=$result['name'];
cuneyt
  • 336
  • 5
  • 15
  • good luck trying to understand how facebook uses curl there. they have thousand different mechanisms. – cuneyt Jan 30 '11 at 18:11