CURL

From HaFrWiki42
Jump to navigation Jump to search

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name originally stood for see URL [1].

The website of cURL opens with: A command line tool and library for transferring data with URLs [2].

What's cURL used for

cURL is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications affecting billions of humans daily.

Who makes cURL ?

cURL is free and open source software and exists thanks to thousands of contributors. The cURL project follows well established open source best practices.

Where's the code?

Check out the latest source code from github.

Practical examples

The examples below are from :

Download a single website

The following command <syntaxhighlight lang="bash" line start="1"> $ curl https://curl.haxx.se </syntaxhighlight> will download the content of the curl website and shows it in the STDOUT.

Save the cURL Output to a file

To save the result of the curl command to a file by using -o/-O options.

  • -o (lowercase o) the result will be saved in the filename provided in the command line
  • -O (uppercase O) the filename in the URL will be taken and it will be used as the filename to store the result

<syntaxhighlight lang="bash" line start="1"> $ curl -o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html

  1. Or

$ curl -O http://www.gnu.org/software/gettext/manual/gettext.html </syntaxhighlight>

Follow HTTP Location Headers with -L option

By default cURL doesn’t follow the HTTP Location headers. It is also termed as Redirects. When a requested web page is moved to another place, then an HTTP Location header will be sent as a Response and it will have where the actual web page is located.

For example, when someone types google.com in the browser from India, it will be automatically redirected to google.co.in. This is done based on the HTTP Location header as shown below. <syntaxhighlight lang="bash" line start="1"> $ curl http://www.google.com

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>302 Moved</TITLE></HEAD><BODY>

302 Moved

The document has moved <A HREF="http://www.google.de">here</A>. </BODY></HTML> </syntaxhighlight>

Pass HTTP Authentication in cURL

Sometime, websites will require a username and password to view the content ( can be done with .htaccess file ). With the help of -u option, we can pass those credentials from cURL to the web server as shown below. <syntaxhighlight lang="bash" line start="1"> $ curl -u username:password URL </syntaxhighlight>

cURL and POST requests

How to make a POST request with the cURL command-line tool? <syntaxhighlight lang="bash" line start="1">

  1. With fields

$ curl --data "param1=value1&param2=value2" https://example.com/resource.cgi

  1. Multipart with fields and a filename:

$ curl --form "fileupload=@my-file.txt;filename=desired-filename.txt" --form param1=value1 --form param2=value2 https://example.com/resource.cgi

  1. Without data:

$ curl --data https://example.com/resource.cgi

  1. or

$ curl -X POST https://example.com/resource.cgi

  1. or

$ curl --request POST https://example.com/resource.cgi </syntaxhighlight>

See for more the excellent documentation on: curl.haxx.se/docs/httpscripting.

PHP Example

<syntaxhighlight lang="php" line start="1">

// Creates a curl resource $c = curl_init();

// Sets the URI curl_setopt($c, CURLOPT_URL, $URL);

// Sets the return the transfer as a string curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

// Sets the return header curl_setopt($c, CURLOPT_HEADER, false);

// Sets the follow location curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);

// Sets the timeout curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 7); curl_setopt($c, CURLOPT_TIMEOUT, 10);

// Sets the Mozila compliance curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");

// Gets the website into a string $response = curl_exec($c);

// Closes the curl curl_close($c); </syntaxhighlight>

See also

top

  • Bookcurl.haxx.se is the home of this book by Daniel Stenberg. It features easy accessible links to read the book online in a web version or download a copy for offline reading using one of the many different versions offered, including PDF, ePUB and MOBI.
  • Book, The HTML-representation of the the Book.
  • curl.haxx.se/docs/httpscripting, How to make post commands and ajax services.
  • SuperUser.com, What is the curl command line syntax to do a post request.

Reference

top

  1. cURL wikipedia
  2. curl.haxx.se] Home page of cURL and libcurl.