Post Picture to Facebook User Wall with PHP

Posting pictures on Facebook works similar as Posting to Facebook Page Wall, you can post not just photos, but questions, status, notes etc in a similar way. In previous post we have seen how we can post a photo to page wall by specifying its relative path, but in this tutorial we will upload picture and directly post to user profile page using an upload form. I have created 3 PHP files in similar manner as before, index.php, config.php and process.php. Index.php contains an image upload field and message box. Once user clicks on upload photo, the data is sent to process.php and if everything seems ok, the uploaded picture will appear in users' profile wall.

Configuration

As before the config.php file contains App ID, App Secret, return url, home url and permissions required. Just replace them with your application details and URLs. Also remember the line 'fileUpload' => true while initializing the SDK, it sets File Upload support in facebook SDK. [cc lang="php"] $appId, 'secret' => $appSecret, 'fileUpload' => true, 'cookie' => true )); $fbuser = $facebook->getUser(); ?> [/cc]

Upload Form

Users are redirected to facebook authentication page, where users grant two permissions publish_stream and user_photos. And then users are presented with upload form, and once user selects a picture and clicks upload, the post data is sent to process.php. [cc lang="html"] destroySession(); header("Location: ".$homeurl); } if(!$fbuser) { $fbuser = null; $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)); echo ''; }else{ ?> Post Photos to User Profile

Post Photos to User Profile

The image will be posted on your profile wall! Reset User Session.

[/cc]

Process & Post Image

The picture file posted from upload form is used as source in $msg_body array, and once we send HTTP POST request to USER_ID/photos, image should appear on user's wall with a message. [cc lang="php"] '@'.$userPhoto, 'message' => $userMessage ); if ($fbuser) { try { $postResult = $facebook->api($post_url, 'post', $msg_body ); } catch (FacebookApiException $e) { echo $e->getMessage(); } }else{ $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)); header('Location: ' . $loginUrl); } //Show sucess message if($fbuser && !empty($postResult)) { echo 'Message Posted'; echo '
'; echo '

Your message is posted on your facebook wall.

'; echo 'Back to Main Page Visit Your Page'; echo '
'; echo ''; } } ?> [/cc] Download Demo
    • Hi deepak, please follow their latest documentation as they have updated their PHP SDK to v 4. This tutorial is bit out-of-date now.
  • Notice: Constant WP_DEBUG already defined in /home2/panpages/public_html/demoapp/wp-config.php on line 83 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home2/panpages/public_html/demoapp/wp-config.php:83) in /home2/panpages/public_html/demoapp/wp-content/themes/motif/inc/facebook.php on line 60 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home2/panpages/public_html/demoapp/wp-config.php:83) in /home2/panpages/public_html/demoapp/wp-content/themes/motif/inc/facebook.php on line 60
  • Hi.. I am getting this error… (#200) Permissions error and i have set permission $fbPermissions = 'publish_actions,user_photos';
  • Please somebody help me. i got this error while i m going to test this demo on my server. (#200) Permissions error and i have set premission email,user_likes,user_about_me,offline_access,publish_actions,manage_pages,status_update,publish_stream,user_photos
New question is currently disabled!