Site icon Web Niraj

Facebook API: Updating the Page Cover Image Programmatically

If you want to update the cover image for your Facebook Page automatically, you can do so using the Facebook PHP SDK. This tutorial shows you how to upload a photo to your Page and then set it as the cover image. You can use this method to automatically rotate between different cover images or promote your business or brand easily.

You’ll need to know your Page Access Token in order to update the cover image. You can find out how to obtain this using this tutorial. Next, you’ll need a cover image we can upload to the page (minimum width is 399px, but recommend size is 851px wide × 315px high) – in my code, I’ve named the image cover.png.

Uploading the Photo

The first step is uploading the photo to the Facebook Page. We do this by first enabling upload support in the PHP SDK (line 10 in code), and setting the page access token (line 13). Setting the access token in this way means all subsequent calls will use this access token (alternatively, you can pass the access_token as a parameter to each call manually).

We upload the photo by making a POST API call to {PAGE_ID}/photos (lines 16-19), passing in the path to the image as source. As my image is in the same directory as the script, I am passing a relative path prefixed with @ (required by Facebook).

If the image has uploaded successfully, Facebook will return an array with the ID of the photo. We’ll need this ID to set the cover image. When a photo is uploaded to a page, an automatic news store is created to inform fans. You can stop Facebook from creating this story by adding the optional no_story parameter to your API call (line 18).

Setting the Cover Image

Once we know the ID of the photo we just uploaded, we need to make a second API call to set the actual cover image, and optionally, its position if it’s larger than 851px width or 315px height. For this, we make a POST API call to {PAGE_ID}, passing in the ID of the uploaded image as cover (lines 22-27).

To prevent Facebook from creating a Cover Image news story on the page, you can pass in the optional no_feed_story parameter. If you plan on changing the cover image often it’s a good idea to disable the functionality to stop spamming your fans.

If the cover image has been changed successfully, the Facebook API will return true, indicating success. You can double-check by visiting your page on Facebook and making sure the new cover image is live.

If you previously uploaded a cover image, you can just change it to that one by using the same API call, but by passing in a different image ID. You can then easily rotate between a number of images by just changing the IDs.

Code

Below is the full code for uploading and setting a photo as the cover image for your Page.

See the gist on github.

[divider style=”dashed”]

Using Remote Images

If you prefer to upload an image that’s stored remotely to your code (for example, an image hosted by a CDN) you can make a small change to your code to accommodate this. Instead of passing the image using the source parameter, you can use the url parameter:

See the gist on github.

Exit mobile version