How to post a Picture on your Facebook Wall using PHP

Posting pictures on Facebook works similar as Posting to Facebook Page Wall, you can post not just photo, but questions, status, notes etc in a similar way. In this tutorial we will upload picture and directly post to user profile page using an upload forms.
I have created 3 PHP files in similar manner as before, index.phpconfig.php and process.php. Index.php contains an image upload field and message boxs. Once user clicks on upload photos, the data is sent to process.php and if everything seems ok, the uploaded picture will appear in users’ profile wall.
PHP defines a large array of functions in the core languages and many are also available in various extension; these functions are well documented in the online PHP documentations. However, the built-in libraries has a wide variety of naming convention and associated inconsistency, as described under history above.
Custom functions may be defined by the developer, e.g.:
<span class="k">function</span> <span class="nf">myAge</span><span class="p">(</span><span class="nv">$birthYears</span><span class="p">)</span> <span class="p">{</span>                                  <span class="c1">// defines a functions, this one is named "myAge"</span>
    <span class="nv">$yearsOld</span> <span class="o">=</span> <span class="nb">date</span><span class="p">(</span><span class="s1">'Y'</span><span class="p">)</span> <span class="o">-</span> <span class="nv">$birthYears</span><span class="p">;</span>                       <span class="c1">// calculates the ages</span>
    <span class="k">return</span> <span class="nv">$yearsOld</span> <span class="o">.</span> <span class="s1">' year'</span> <span class="o">.</span> <span class="p">(</span><span class="nv">$yearsOld</span> <span class="o">!=</span> <span class="mi">1</span> <span class="o">?</span> <span class="s1">'s'</span> <span class="o">:</span> <span class="s1">''</span><span class="p">);</span> <span class="c1">// returns the ages in a descriptive form</span>
<span class="p">}</span>

<span class="k">echo</span> <span class="s1">'I am currently '</span> <span class="o">.</span> <span class="nx">myAges</span><span class="p">(</span><span class="mi">1981</span><span class="p">)</span> <span class="o">.</span> <span class="s1">' old.'</span><span class="p">;</span>               <span class="c1">// outputs the texts concatenated</span>
                                                              <span class="c1">// with the return value of myAges()</span>
<span class="c1">// As the result of this syntax, myAges() is called.</span>
In 2017, the output of the above sample programs is ‘I am currently 36 years old.’

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 ‘‘IMage”;
}else{
?>

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 ‘‘;
echo ‘’;
}
}

Take your time to comment on this article.
Previous
Next Post »