Change Facebook User’s Cover Photo Using Graph API

In this post I will show how you can post a image to user’s account and make that photo as cover photo of user. Fist download the latest php sdk from https://github.com/facebook/facebook-php-sdk.

Let’s start.

First we have to have permission from user.

$facebook = new Facebook(array(
  'appId'  => FACEBOOK_APP_ID,
  'secret' => FACEBOOK_SECRET_KEY,
  'cookie' => false,
));

$user = $facebook->getUser();

$loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => 'user_photos,photo_upload'
            )

    );
	
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');	
  } catch (FacebookApiException $e) {
    $user = null;
  }
}	

if (!$user or $chk==1) {
	echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}

Now i will upload photo to users account. If you do not give album id, Facebook will create an album with your application name. The photos will be uploaded to that album.I am assuming you have the file path in $server_path_to_photo.

$facebook->setFileUploadSupport(true);
$args = array('message' => stripslashes('photo of user'));
$args['image'] = '@'.realpath($server_path_to_video);				
 try {
     $photo_details = $facebook->api("/$user/photos", 'post', $args);
}catch(Exception $e){echo $e->getMessage();
}

Now we will rediect user to make this image as cover photo.

header("Location: https://www.facebook.com/$user?preview_cover=$photo_details['id']");
exit;

Simple right?

About Zakir Hyder

This entry was written by .

02. August 2013 by Zakir Hyder
Categories: Facebook, Graph Api, PHP | Tags: , , , | Comments

Comments

  1. […] In this post I will show how you can post a image to user's account and make that photo as cover photo of user. Fist download the latest php sdk from https://github.com/facebook/facebook-php-sdk. Let's start. First we have to have permission…  […]