[SOLVED] PHP upload photo to users facebook wall using php only


I am trying to write a php script that can upload a daily photo to my personal facebook wall. I need this to be done at anytime and not just when I am logged in. I am wondering if it is possible to upload a photo to my wall using php only or do I need to use an app with the right permissions. If so do I have to personally install that app and give correct permissions for my page to make this possible, if it is possible, and can this be done automatically?

What I want to do:
In short I want a users twitter feed to be converted and put on an image. I have the script that automatically does this already. However I would love it if after the twitter user tweets then this calls my script and converts it to an image (again I have done this code already) but…

Now I need a script that can automatically upload this to my personal wall after the image is created. Is this possible?


Well, you can use the extended user access token to get it working for a maximum of 60 days. After that, the token will expire and then you’ll have to refresh the token and that is not possible without the user’s interaction.

The documentation states the same:

Even the long-lived access token will eventually expire. At any point, you can generate a new long-lived token by sending the person back to the login flow used by your web app – note that the person will not actually need to login again, they have already authorized your app, so they will immediately redirect back to your app from the login flow with a refreshed token – how this appears to the person will vary based on the type of login flow that you are using, for example if you are using the JavaScript SDK, this will take place in the background, if you are using a server-side flow, the browser will quickly redirect to the Login Dialog and then automatically and immediately back to your app again.

After doing the above you will obtain a new short-lived token and then you need to perform the same exchange for a long-lived token as above.

To get the extended token make the following call from your server:

GET /oauth/access_token?  


$app_id = APP_ID;
   $app_secret = APP_SECRET;
   $post_login_url = APP_URL;
   $album_name = 'test';
   $album_description = 'desc';

   $code = $_REQUEST["code"];

   //Obtain the access_token with publish_stream permission 
       $dialog_url= "http://www.facebook.com/dialog/oauth?"
       . "client_id=" . $app_id 
       . "&redirect_uri=" . urlencode($post_login_url)
       . "&scope=publish_stream";
       echo("<script>top.location.href='" . $dialog_url . 
   else {
     // access token
     $token_url= "https://graph.facebook.com/oauth/"
     . "access_token?"
     . "client_id=" .  $app_id 
     . "&redirect_uri=" . urlencode( $post_login_url)
     . "&client_secret=" . $app_secret
     . "&code=" . $code;
     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);
     $access_token = $params['access_token'];       // <---
     // extended token
     $extended_token_url= "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=".$app_id."&client_secret=".$app_secret."&fb_exchange_token=".$access_token; 
     $response = file_get_contents($extended_token_url);
     $params = null;
     parse_str($response, $params);
     $extended_access_token = $params['access_token'];  //  <---

     // Upload to the photos to the album here

That’s the complete auth process, incl the extended token. You can find many posts regarding the upload of the photo, simply use the $extended_access_token with your calls.

(In case you are not aware, Graph API Explorer is a fantastic tool of facebook to test the APIs).

Good luck!

Answered By – Sahil Mittal

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.