Change Facebook Page’s Cover Photo Using Graph API and Page API

Update:After recent changes to Facebook api, we can only make cover with photo which is in an album. Previously we can make cover from photos of page’s album. I updated the code. Now we have to create an album and upload the photo to that album then we can use that photo as cover photo. Previously we can make cover from “photos of page’s” album.



Facebook added new Page API to change the cover photo of page. To able to do this you need to ask manage_pages and user_photos permissions. manage_pages permission to get page access token and user_photos to upload photos to page. Facebook does not allow to upload image to as cover photo – you have to upload a photo in an album and then give that photo id as cover photo. Download the Facebook SDK for PHP.

First we will ask our user to login with manage_pages and user_photos permission.

require 'facebook.php';

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

$user = $facebook->getUser();

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

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


if (!isset($permissions['data'][0]['user_photos']) 
or !isset($permissions['data'][0]['manage_pages']) ) 
{
  $user = null;
}

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

To work with page API, we need to get page access_token. We can get using /me/accounts graph api. We need to setFileUploadSupport true as we are uploading images. In this example I am just using a image located on the server. Then we are going to set a cover photo for a Page by issuing an HTTP POST to /PAGE_ID with a cover parameter.

$facebook->setFileUploadSupport(true);
$accounts = $facebook->api('/me/accounts');
for($i=0;$accounts['data'][$i];$i++){
    $page_access_token=$accounts['data'][$i]['access_token'];
    $page_id = $accounts['data'][$i]['id'];

    $facebook->setAccessToken($page_access_token);
    $args = array('name' => 'awesome album name', 'message' => 'awesome album message');
    try {
        $album_id =$facebook->api("/$page_id/albums", 'post', $args);
    }catch(Exception $e){
        echo $e->getMessage();
    }

    $args = array('image' =>'@'.realpath('/var/www/facebook.png'));
    try{
        $uploaded_photo_details = $facebook->api("/{$album_id['id']}/photos", 'post', $args);
    }catch(Exception $e){
        echo $e->getMessage();
    }

    if(isset($uploaded_photo_details['id'])){
        $args = array('cover' => $uploaded_photo_details['id'], 'offset_y' =>0);
         try {
             $cover_details = $facebook->api("/{$page_id}", 'post', $args);
        }catch(Exception $e){
            echo $e->getMessage();
        }
    }
}

About Zakir Hyder

This entry was written by .

05. December 2012 by Zakir Hyder
Categories: Facebook, Graph Api | Tags: , , , | Comments

Comments

  1. […] Update:After recent changes to Facebook api, we can only make cover with photo which is in an album. Previously we can make cover from photos of page's album. I updated the code. Now we have to create an album and…  […]