Sending HTTP POST with php cURL

As promised previously, I’m going to show you how to send HTTP POST request using php cURL extension.

The target form

Let’s say you have a html form like this :

wtf

And this is the source code of the html file :


You can see that the form will submit the query using HTTP POST to “target.php”. Now let’s say you want to write a php script (bot.php) that will automatically send the query bypassing the html form, this is one way to do it (with php libcurl extension)

< ?php
//bot.php
$url = "http://localhost/wtf/target.php";
$ch = curl_init();

// set the target url
curl_setopt($ch, CURLOPT_URL,$url);

// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// the parameter 'username' with its value 'johndoe'
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=johndoe");


$result= curl_exec ($ch);
curl_close ($ch); 
print $result;

?>

This script will send a HTTP POST request to “target.php” pretending to be a real person sending the “username” parameter as “john doe”.

However this is not entirely convincing since the server side will automatically know that you are using a http script to send the HTTP POST request by analyzing the browser “user-agent” string. The default script will send “(HTTPRetriever/1.0)” as its user-agent.

With a little add-on, you can spoof the user-agent string inside your script just like this :

< ?php
//
// test HTTP POST submitter, using libcurl
//

// the target url which contains scripts that accepts post request
$url = "http://localhost/wtf/target.php";

// we are spoofing Yahoo Seeker bot >:)
$useragent="YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";

$ch = curl_init();

// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// set the target url
curl_setopt($ch, CURLOPT_URL,$url);

// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// the parameter 'username' with its value 'johndoe'
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=johndoe");


// execute curl,fetch the result and close curl connection
$result= curl_exec ($ch);
curl_close ($ch); 

// display result
print $result;

?>

so when your “bot.php” sends the request, the server logs will record that the query was sent by a “Yahoo Seeker bot” instead of a crudely coded php script.

You can spoof other browser as long as you know their user-agent string, refer to my previous post for a collection of browser user-agent strings.

No PHP cURL support?

In this case, you have a few options

  1. Use a server that support php cURL extension
  2. Compile/Install php cURL extension
  3. Use libcurlemu – php cURL extension written in pure php

Well that should cover the short crash course on how to use php cURL extension.

p/s : Although I won’t tell you how to write one directly, this is the basic of building spam bots and auto-submitter. So use your imagination (and the dark side of the force) to write the rest of the code. *evil*

You can download the source code of this tutorial here : http://mypapit.net/pub/libcurltest.zip

php,curl,webdev,libcurl,bots

A rather large collection of browser User-agent strings

If you are into analyzing http log files or writing web applications that does user-agent analysis, you might appreciate this website PGTS Agent String Switchword. The website has a huge collection of browser user-agent list ranging from the common Mozilla/Internet Explorer variant to search-engine spider, malicious web bots and internet worms.

The list is organised in various orders to ease up browsing (alphabetical, operating system, by popularity, common robots user agent). Finally, the site also offers tab-delimited user-agent download (zip file). This is useful for those who want to build user-agent database for their own project.

How to Recover Scratched CDs


scratched cds
Ever been in situation where you need to install softwares from an old CD-ROM, or perhaps retrieve backups of your works? It must be a frustrating experience when you found out that your data couldn’t be retrieved because the CDs has been scratched. While preventing scratches may avoid this sort of problem, let face it, scratches may appear on your well stored CD/DVD as well.

The article I found below discussed on ways to recover from scratched CD-ROM, with a few tips and guides how to look for scratches to the final step of recovering data from it. Trust me, you may need this tip someday : Recovering Scratched CDs

cd, cd-rom, dvd, recovery,computer

Guido van Rossum – fun playing Python on S60 (pys60)

Nokia 6630 MypapitThe Python for Nokia is really exciting from the moment you download it, to the moment it runs on your phone. I know that experience first hand when I saw my Python applications was up and running on my phone! Previously I only had experience writing mobile applications in Java J2ME, but writing the same stuff in Python is a whole new experience for me. It seems that I’m not the only one having fun with Python for Nokia. Python creator Guido van Rossum, also states in a forum that he has too much fun with Nokia 6630 phone that he recently acquired.

He expressed that Nokia has done an outstanding job for porting Python to its own product line, incorporating Python library inside the phone. Additionaly, extensions also exists to handle phone’s operation like dial a call, snap a picture, send/receive SMS, Bluetooth, and Internet.

Finally, Nokia has made writing Symbian applications even easier with GUI modules that handles menu, alert, tab, canvas, event loop and other low-level event without the need of recompiling application. Python for Nokia is indeed fun. Why don’t you try it yourself now?

Original forum thread : http://www.artima.com/forums/

symbian,nokia,python,pys60,s60,mobile