Picture sharing on Facebook with PHP

If you have more than one website and their facebook pages, management and sharing of these pages can be tedious and time consuming.

I have found a solution to this problem. I prepared the codes with the Postman.

Setup

  • Instead of %PICTURE_URL% type the image address to be shared.
  • Instead of %MESSAGE% write a post to share with picture .
  • Instead of %FB_TOKEN%, type facebook access token.
  • If you want to automate the tasks completely, you can add it to your cron jobs section of your hosting control panel after you have done the following by yourself:

/usr/bin/php -q /home/hostname/public_html/directory/file.php

Facebook Token Receiving

I skip creating a facebook application, assuming you already have a facebook app.

  • Go to https://developers.facebook.com/tools/explorer/ and grab your token. If you are going to share on the page, be sure to give the publis_pages permission.
  • Extend your token membership by visiting https://developers.facebook.com/tools/debug/accesstoken/. When the time is over, you will have to extend again.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.facebook.com/v2.8/me/photos",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"url\"\r\n\r\n%PICTURE_URL%\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"caption\"\r\n\r\n%MESSAGE%\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"access_token\"\r\n\r\n%FB_TOKEN%\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 60ed2a4c-3ed3-ef9c-188e-46a517c1bf4e"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

 

Leave a Reply

Your email address will not be published.