by Marc Plotz
$ cURL -V
curl 7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
Protocols: tftp ftp telnet dict ldap http file https ftps
Features: GSS-Negotiate IPv6 Largefile NTLM SSL libz
$ cURL www.google.com
$ curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.za/">here</A>.
</BODY></HTML>
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$ch = cURL_init("http://www.example.com/"); // Init and tell CURL where we will be visiting.
$fp = fopen("example.txt", "w"); // Open a file for writing.
cURL_setopt($ch, CURLOPT_FILE, $fp); // Tell CURL we're writing to $fp
cURL_setopt($ch, CURLOPT_HEADER, 0); // Do not include a header in the output
cURL_exec($ch); // Execute!
cURL_close($ch); // Close - shut down CURL, we're done.
fclose($fp); // Close the file for reading.<?php
$status = 'Hello Facebook!'; $email = 'myemail@facebook.com'; $pass = 'mypassword';
$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.
$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!