Curl Cheat Sheet
Curl from command line
Curl is a way of making web requests from the command line. I only do this under linux, and if you don’t know why you’d want to do that, then you probably don’t need to read any further (although, that would also make an interesting article!).
curl http://www.lornajane.net
Will give you the output that would be sent to your browser if you requested the main page of this site from there.
Verbose output (-v)
This is where it gets interesting, you can see the actual information being included with the HTTP request when you use the verbose switch.
curl -v http://www.lornajane.net
Try it – you see the headers and the client/server negotiation. Stuff like cache information, browser identification, and cookie information all gets transmitted in these headers.
Using HTTP verbs (-X) and data (-d)
Curl can make requests and include data with them. It can use GET as you’d expect, but POST, PUT and DELETE are all supported too.
curl -X GET "http://www.example.com?page=2&category=toys"
I’ve added the quotes because the ampersand confuses the command line
curl -X POST http://www.example.com/registration.php -d username=lornajane -d password=password -d phone=0123456789
PUT and DELETE can be done in the same way, just using the -X switch.
Further Resources
Actually, if you’ve got this far then you know what you’re doing – use the curl manpage for information on the other switches. Curl can do more things than I think I would ever want to do, not just HTTP, and can spoof cookies and all sorts of other good things. Its especially good for testing web services as it allows clearer diagnosis of the exact behaviour and response. Don’t tell anyone but I have also been known to vardump in the middle of webservices when debugging, and using curl lets you see the output “as it comes” – very handy!
Update: I wrote a follow-up post about Using Cookies with Curl
One other useful switch is -H to pass arbitrary request headers, eg -H “X-Foo: bar”. I’m sure there’s a better way, but I usually end up switching things like user-agent (and other things) like this
Thanks lorna, I’m new to curl and I just heard that “exists” in PHP. Your article was very helpful.
Kevin: that’s a good addition, thanks for that :)
Robert: I’m very pleased to hear this was helpful. You can use curl from within PHP itself, I’ve been doing this recently and will write another post on that some time
Lorna,
Great post, found this via Chris’s blog, more tools in my toolset now :). Thanks
Vid: Thanks for dropping by, I’m very pleased to hear you found this useful.
Having recently written articles about curl and about writing a PHP REST server, I thought I’d complete the circle and put here a few notes on using PHP’s curl wrapper as a Rest client. Also I had to look some of this up when I needed to actually do it s
curl is the C URL library – its a command-line tool for making web requests, with libraries available in many languages. Personally I prefer to use it from the command line and recently I have been using it with cookies for a web services which set a coo
Thanks a lot, very helpful! Bookmarked!
Pingback: Returning JSON using the Accept header in ZF2 – Rob Allen's DevNotes
thank you, really useful! :)
Pingback: Returning JSON using the Accept header in ZF2 | Rob Allen's DevNotes