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

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

libcurlemu – a pure PHP curl implementation (libcurl emulator)

Have you came across a situation where you need to use php libcurl extension and it was not enabled on your webserver? Then, try libcurlemu, apure-PHP implementation to PHP cURL. It emulates all of the curl_* functions normally provided by the native cURL extension itself.

libcurlemu works transparently regardless of the availability of PHP cURL extensions, thus making your script even more portable across web server that does not have PHP cURL extensions.

What is libcurl / cURL?

cURL is a command-line tool that supports transfering files with URL syntax. It supports many protocols like FTP, FTPS, TFTP, HTTP, HTTPS, TELNET, DICT, FILE and LDAP.

libcurl is a library based on cURL that helps you to connect and communicate with different type of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, and ldap protocols.

For HTTP/HTTPS protocol, libcurl supports POST, PUT, GET and DELETE method, making it suitable for writing web services, particularly REST-like services.

Stay tune for code examples on how to use libcurl to emulate a browser submitting POST request to a server.

libcurlemu is written by Steve Blinch, and can be downloaded at his website : http://code.blitzaffe.com

php, libcurl, curl, rest, opensource

Create your own Debian/Ubuntu (*.deb) package

mypapit debianI’ve found a nice well-written guide that may be useful for those who wants to create Debian *.deb package. Though compiling applications/software/libraries right from sources are desireable for some people, there’s still lots of others who prefer to install stuffs software packages for conveniency.

Why do you want to create Debian (*.deb) packages

There’s many reasons for you to create *.deb packages, one of it if you are (or want to be) a maintainer for a Debian-based project, or merely maintaining a unofficial packages for Debian-based system.

The guide would be useful if you are a software developer or in charge of distributing softwares in pre-package form to various GNU/Linux distribution. Creating a prepackage *.deb files will help people install your software without the need of developments tools and save compiling time, this is important since there are people who like to manage installations using software management tools such as synaptic and aptitiude.

Though most people would prefer to distribute binary only *.deb package, the practice of distributing pre-packaged source codes in *.deb format is not uncommon in Debian-based distro scene.

Here’s the link to the guide that I’m talking about :

  1. Rolling your own Debian packages (part 1)
  2. Rolling your own Debian packages (part 1)

Other Links :

  1. Debian New Maintainer website
  2. Debian Binary Package Howto
  3. Create Debian GNU/Linux Package

debian, ubuntu, linux, package, deb, repository, repositories

Mandriva Starter Guide Wiki

Mandriva Linux LogoSeems that Mandriva Linux users doesn’t need to spend hours of unproductive work trying to figure how to work things out for their Mandriva Linux system.

The fine folks at Linux Center of University of Latvia have started an Unofficial Mandriva Linux Starter guide in spirit to help fellow Mandriva Linux users across the world. The guide itself is organised in a wiki, and everyone else are encouraged to contribute.

The website contains many useful links and guides that could be helpful for new Mandriva Linux users. Among the guides and tips included :

  1. Where to download Mandriva Linux
  2. Where to look for new programs
  3. How to Install Java 2 Runtime Environment Plugin in Mozilla Firefox
  4. Configuring Network Connections
  5. How to add extra Repositories in Mandriva Linux
  6. How to Add/Delete Users and groups
  7. How to watch DVD in Mandriva Linux
  8. And many more…

The same folks that brought you this wiki is currently also working to release Unofficial Gentoo Linux Wiki.

Hope that these links will be helpful to you !!

linux, mandrake, mandriva, repositories, wiki

Instalinux – A new approach to get GNU/Linux

Downloading GNU/Linux usually involves a process of downloading the entire CD image (called iso). This sometimes can be a time consuming process if you only need to install GNU/Linux with a few desired applications, as ISO provided by GNU/Linux distro is bundled with lots of applications that you wouldn’t ever use. It would be nice to have an ISO that would be tailor-made to your needs with all unessary applications removed to lighten the download.

Furthermore, the installation process for most GNU/Linux distribution is too generic, where a user has to attend the installation by manually answering questions such as Languange preference, Timezone, Keyboard Layout, Mouse, etc. This can be a bit of an annoyance if the user would wish install the GNU/Linux on multiple computers, usually those settings will remain the same for the user (how many are you have changed those preference?).

Instalinux

Instalinux (http://www.instalinux.com/ ) has taken a new approach to provide customized installation CD to the power users. Based on the LinuxCOE project by Hewlett Packard, Instalinux solves the problem of “generic ISO image” by providing a way to the user to create a custom installation disc by his/her own preference.

Users can choose from Fedora, SuSE, Debian and Ubuntu, select their system parameters, select software packages and pick a public mirror to install from, and then download the ISO image that is generated.

All of these are obtainable by the means of Kickstart, AutoYAST, or the Debian Preseed autoinstallers through Instalinux interface.

fedora, instalinux, linuxcoe,linux,gnu+linux,suse,tips