36030

How to access facebook profile picture using facebook php SDK?

Question:

I am using the following code to access users profile picture.

<strong>This is how I created the request</strong>

<?php session_start(); include 'facebook-php-sdk/autoload.php'; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\FacebookSession; $permissions = array( 'email', 'user_location', 'user_birthday', 'user_photos', 'public_profile' ); /* Set the public and secret keys */ FacebookSession::setDefaultApplication('XXX', 'XXX'); $helper = new FacebookRedirectLoginHelper('http://localhost/oauth/login.php'); $loginUrl = $helper->getLoginUrl($permissions); // Use the login url on a link or button to redirect to Facebook for authentication ?> <a href="<?php echo $loginUrl; ?>">Login using facebook</a> <?php

?>

<strong>And this is the page user is redirected to..</strong>

<?php session_start(); include 'facebook-php-sdk/autoload.php'; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\FacebookSession; FacebookSession::setDefaultApplication('XXXX', 'XXXX'); $helper = new FacebookRedirectLoginHelper('http://localhost/oauth/login.php'); try { $session = $helper->getSessionFromRedirect(); } catch(FacebookRequestException $ex) { // When Facebook returns an error } catch(\Exception $ex) { // When validation fails or other local issues } if ($session) { // Logged in // $request = new FacebookRequest($session, 'GET', '/me'); // $response = $request->execute(); // $graphObject = $response->getGraphObject(); // var_dump($graphObject->asArray()); $request = new FacebookRequest( $session, 'GET', '/me/picture' ); $response = $request->execute(); $graphObject = $response->getGraphObject(); var_dump($graphObject->asArray()); }

?>

I am always getting output as "array(0) { }" -> an empty array. I cant figure out whats wrong with this code, did I missed some permission while getting login url or anything wrong with the graph query??

<strong>And here's the vardump of $response object</strong>

It Workedobject(Facebook\FacebookResponse)#10 (5) { ["request":"Facebook\FacebookResponse":private]=> object(Facebook\FacebookRequest)#4 (6) { ["session":"Facebook\FacebookRequest":private]=> object(Facebook\FacebookSession)#8 (2) { ["accessToken":"Facebook\FacebookSession":private]=> object(Facebook\Entities\AccessToken)#3 (3) { ["accessToken":protected]=> string(221) "CAAFIKIRoZBB0BAHnkev9CzhEZA0iOCauwOQya3bz5ZBwxzG4jEM3CyK8iU4QRcZB1fdGSfBfClLSCMbGhKiguMZBTiXZB2imzZCf8XLzifaJsqjXufJ5Y K9SxkZBboZCBWvOda4CDSNwvOf6rFW2D5qqZBZC7nFUb1tWMFTfkavkAWntBRjN6O3vZAnqkISkmpGqGqphx3JR99ca3G9gIxTiMlAn" ["machineId":protected]=> NULL ["expiresAt":protected]=> NULL } ["signedRequest":"Facebook\FacebookSession":private]=> NULL } ["method":"Facebook\FacebookRequest":private]=> string(3) "GET" ["path":"Facebook\FacebookRequest":private]=> string(3) "/me" ["params":"Facebook\FacebookRequest":private]=> array(2) { ["access_token"]=> string(221) "CAAFIKIRoZBB0BAHnkev9CzhEZA0iOCauwOQya3bz5ZBwxzG4jEM3CyK8iU4QRcZB1fdGSfBfClLSCMbGhKiguMZBTiXZB2imzZCf8XLzifaJsqjXufJ5YK9 SxkZBboZCBWvOda4CDSNwvOf6rFW2D5qqZBZC7nFUb1tWMFTfkavkAWntBRjN6O3vZAnqkISkmpGqGqphx3JR99ca3G9gIxTiMlAn" ["appsecret_proof"]=> string(64) "e30042c6689e1ca31de1057f700c95f37dda509544fed8615cc413ae085e5c08" } ["version":"Facebook\FacebookRequest":private]=> string(4) "v2.0" ["etag":"Facebook\FacebookRequest":private]=> NULL } ["responseData":"Facebook\FacebookResponse":private]=> object(stdClass)#7 (13) { ["id"]=> string(15) "703241603086465" ["birthday"]=> string(10) "08/13/1988" ["email"]=> string(19) "smit.new1@gmail.com" ["first_name"]=> string(8) "Saumitra" ["gender"]=> string(4) "male" ["last_name"]=> string(3) "Dey" ["link"]=> string(60) "https://www.facebook.com/app_scoped_user_id/703241603086465/" ["location"]=> object(stdClass)#9 (2) { ["id"]=> string(15) "110561898965802" ["name"]=> string(13) "Tezpur, Assam" } ["locale"]=> string(5) "en_US" ["name"]=> string(12) "Saumitra Dey" ["timezone"]=> float(5.5) ["updated_time"]=> string(24) "2014-09-12T00:44:34+0000" ["verified"]=> bool(true) } ["rawResponse":"Facebook\FacebookResponse":private]=> string(389) "{"id":"703241603086465","birthday":"08\/13\/1988","email":"smit.new1\u0040gmail. com","first_name":"Saumitra","gender":"male","last_name":"Dey","link":"https:\/\/www.facebook. com\/app_scoped_user_id\/703241603086465\/","location":{"id":"110561898965802","name":"Tezpur, Assam"},"locale":"en_US","name":"Saumitra Dey","timezone":5.5,"updated_time":"2014-09-12T00:44:34+0000","verified":true}" ["etagHit":"Facebook\FacebookResponse":private]=> bool(false) ["etag":"Facebook\FacebookResponse":private]=> string(42) ""895f82630994e22ce349e5ab8315fe8a9c0665e1"" }

Answer1:

I expected some default behavior but turns out that extra option is required for the query to be successful.

$request = new FacebookRequest( $session, 'GET', '/me/picture', array ( 'redirect' => false, 'type' => 'large' ) );

Recommend

  • How to return a HashTable from a WebService?
  • yii2 create translated URLs
  • Use double quote then curly brace in powershell -Command
  • Active Directory Powershell - forest-wide search script using .csv list of users
  • PHP exec response as string?
  • How to include Web reference endpoint configuration in another project
  • php notice undefined index http_host in $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'
  • Index.php as custom error page
  • read values from form post in jquery or javascript
  • Returning the auto incrementing value after an insert using slick
  • (Tcl/Expect) clear screen after exit
  • hibernate sets dirty flag (and issues update) even though client did not change value
  • NHibernate manually control fetching
  • How to copy styled text in JTextPane
  • Why does it draw lines in the wrong place?
  • Unable to send e-mail through Java
  • Salesforce Different WSDL files and when to use
  • SQLite connection strategies
  • How can I extract results of aggregate queries in slick?
  • Converting a WriteableBitmap image ToArray in UWP
  • Django rest serializer Breaks when data exists
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Recording logins for password protected directories
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Why value captured by reference in lambda is broken? [duplicate]
  • Is there any way to access browser form field suggestions from JavaScript?
  • Different response to non-authenticated users and AJAX calls
  • javascript inside java/jsp code
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • A cron job substitute?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Getting error when using KSoap library to consume .NET web services
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF