Curl Send Post



Curl send post commandCurl post request with parametersWritten by Mark Sanborn: Aug 7, 2008Curl Send Post

This method is very similar to POST, but we use it when we want to send a new version of an existing resource. In order to do this, we use the -X option. Without any mention of a request method type, curl defaults to using GET; therefore, we explicitly mention the method type in the case of PUT. When talking to a HTTP 1.1 server, you can tell curl to send the request body without a Content-Length: header upfront that specifies exactly how big the POST is. By insisting on curl using chunked Transfer-Encoding, curl will send the POST chunked piece by piece in a special style that also sends the size for each such chunk as it goes along. In this Post I'll explain you how to send a GZIPed post body using CURL commands. Its pretty straight forward, noting down if anyone come accross a similar requirement. First you need to create the body you need to send, In my case I'm sending a XML content. Note that you do have to provide the path to a curl executable, or at least specify curl.exe; curl by itself is a Powershell alias for the Invoke-WebRequest cmdlet, which can do most of what the cURL program can do but has very different argument syntax.

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:

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.

Curl

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.

Curl Send Post Request

Share: