Sending Post Data With cURL

Written by Mark Sanborn: Aug 7, 2008

I have a script on my local computer that needs to send data to my website for further processesing. To accomplish this I use a powerful tool called cURL. cURL is an open source program that lets me send or recieve HTML data in pretty much any structure you could think of. I am going to use cURL to send POST data to a PHP script on the site.

Make sure you have cURL.

curl -V

This should print out the version of cURL you have. Note their are some bugs in older versions of cURL that doesn’t handle DNS very well and can cause your scripts to slow down. Make sure you are up to date.

To Install cURL on a debian based system do:

apt-get install curl

First thing you will want to do is test your cURL by just pointing it to your website.

curl http://www.marksanborn.net/

If everything is well curl should output all of the HTML to your screen.

Next thing you will want to do is setup your PHP script to accept POST data. A simple script might look something like this:

<?php
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$value3 = $_POST['value3'];

if ($value1 == 'foobar') {
    // Do something with it.
} else {
    echo 'Invalid Request';
}
?>

Of course if you are doing any MySQL data exchanges or anything else of that nature you will want to sanatize the data before using it.

Then to send the data with cURL you send:

curl -d "value1=foo&value2;=foo2&value3;=foo3" http://www.marksanborn.net/someScript.php

Real world example

For those of you that are wondering what you could do with this, I will give you a real world example.

I built a BASH script that will send the tracking number, ship method, and order number to Google checkout. Google checkout will then process the order, send an email out to the customer, and archive the order. In other words, after every label is printed from the local shipping computer it runs a BASH script with the tracking number and the BASH script sends all the info up to Google.

You could use this for any website that has post fields (as long as it doesnt have a captcha). So you could send automatic twitter messages or interact with Google services.

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Share: