Update Your Facebook Status with PHP and cURL
Let’s move on by updating our Facebook status. We are going to use a very cool script to update our Facebook status. What we are going to need to do to get it to work properly is log in to Facebook, update our status, and then log out. We are going to handle all of this with cURL in four steps.
Let’s set up the basics. We’ll need to know our Facebook email address, password, and the status we want to post. Of course we can post the status from a form if we want to be fancy.
<?php
$status = 'Hello Facebook!'; $email = '[email protected]'; $pass = 'mypassword';
Log in to Facebook:
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "mycookie.txt");
// Fake your cookie collection
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
// Fake your browser
curl_setopt($ch, CURLOPT_URL,"http://m.facebook.com/");
// Tell CURL where we are going
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Tell CURL to return whatever comes back
$fbhome = curl_exec ($ch);
// Execute!
preg_match("/<form method="post" action="(.*)">/U", $fbhome, $formaction);
// find the form on the page
urlencode(preg_match("/<input type="hidden" name="charset_test" value="(.*)" />/U", $fbhome, $chartest));
preg_match("/<input type="hidden" name="post_form_id" value="(.*)" />/U", $fbhome, $formid);
curl_close ($ch);
// Kill the login part of the CURL session
unset($ch);
$ch = curl_init(); // Start a NEW CURL SESSION
curl_setopt($ch, CURLOPT_COOKIEFILE, "mycookie.txt");
// Fake the cookie collection
curl_setopt($ch, CURLOPT_COOKIEJAR, "mycookie.txt");
// Fake the cookie file
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
// Fake the browser
curl_setopt($ch, CURLOPT_URL, $formaction[1]);
// POST THE FORM
curl_setopt($ch, CURLOPT_POST, 1);
// Set the method to POST
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "post_form_id=".$formid[1]."charset_test=".$chartest[1]."&email=$email&pass=$pass&login=Log+in");
// Add some fields to the form. Facebook requires these.
$loggedin = curl_exec ($ch);
// Execute the Facebook Login
preg_match("/<form method="post" id="composer_form" action="/a/home.php(.*)">/U", $loggedin, $formaction);
preg_match("/<input type="hidden" name="fb_dtsg" value="(.*)" autocomplete="off" />/U", $loggedin, $dtsg);
preg_match("/<input type="hidden" name="post_form_id" value="(.*)" />/U", $loggedin, $formid);
curl_close ($ch);
unset($ch);
// We're logged in, kill the current CURL SESSION.
And finally, post the status.
$ch = curl_init(); // We pretty much do the same again - I could put this in a function, but for clarity will not.
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/a/home.php'.$formaction[1]);
// POST THE FORM
curl_setopt($ch, CURLOPT_POST, 1);
// Make sure it's set to POST.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "charset_test=".$chartest[1]."&fb_dtsg=".$dtsg[1]."&post_form_id=".$formid[1]."&status=$status&update=Share");
// Send the status in the URL, via GET
$buf2 = curl_exec ($ch);
curl_close ($ch);
unset($ch);
// Kill it, we are done!
In conclusion, cURL is a very powerful tool that allows us to not only get but also send information to and from a server. Used correctly, we can very easily manipulate websites such as Facebook to do our bidding. Please remember though — always use responsibly.
Until next time,