Hosting with cURL
13:40, 29.11.2022
If you have decided to automate data transmission on the network using a variety of protocols, this article will help. This software works with protocols:
- FTP;
- FTPS;
- HTTP;
- HTTPS;
- TFTP;
- SCP;
- SFTP;
- Telnet;
- DICT;
- LDAP;
- POP3;
- IMAP;
- SMTP.
Working with cURL can only be done through the command line, as there is no user interface. It is also possible to use any programming language you are working with as an additional module.
Features of the cURL library
The utility is available for all modern Unix-based operating systems. This command is used to retrieve data from a given URL or to transfer data via the protocols listed above. The utility requires the Libcurl API library, which is necessary to be able to transfer files to multiple applications.
Libcurl is a robust and portable open-source library that can easily be integrated into any application. The cURL command itself can be used on most Linux distributions for such purposes:
- API testing.
- Using inside shell scripts.
- Providing access to files without a browser.
The package can be used both for commercial and non-commercial purposes - the source code is open and the software is distributed freely.
Installing cURL
Windows users must first install the PHP module because without it the utility will not work. Next, to install cURL, you need to download the installer from the official website of the developers. Just choose the appropriate version for your OS and then install it.
For any other operating system, you can also find the installer in the corresponding list. There are versions for Linux, FreeBSD, DOS, Debian, Ubuntu, Fedora and many other operating systems – both free and those aimed at corporate use.
How to work with cURL
There are many commands that this utility understands. There are so many that we can't physically cover them all – that would require detailed multi-page documentation. You can find it on specialized forums or on the site of the developers, we will go over the syntax. It is fairly simple and looks like this:
$ curl options link
What is a link – it is anyway obvious, but the possible options are quite a lot:
- -# – show loading progress;
- -0 – switches to HTTP 1.0 protocol;
- -1 – initiates the use of tlsv1 encryption protocol;
- -2 – switch to sslv2;
- -3 – switch to sslv3;
- -4 – changeover to ipv4;
- -6 – changeover to ipv6;
- -A – specify your USER_AGENT;
- -b – save cookieі as a file;
- -c – transfer cookies to the server from the file;
- -C – resume downloading the file from the location of the break or specified offset;
- -m – the waiting time limit for the server's response;
- -d – transmit data by POST method;
- -D - saves the headers returned by the server to a file;
- -e – set the Referer-uri field, indicating from which site the user came;
- -E – initiates the use of external SSL certificate;
- -f – forbids outputting the Error-type messages;
- -F – transmit data as a form;
- -G – activation of this option initiates the transfer of all data specified in option -d, by means of the GET method;
- -H – send headers to the server;
- -I – receive HTTP header ignoring page content;
- -j – read and send cookies from the file;
- -J – removes the header from the request;
- -L – receive and process redirects;
- -s – show the maximum number of redirects with Location;
- -o – output page content to a file;
- -O – store content to a file with the name of the page or file on the server;
- -p – use proxy;
- --proto – allows you to set the preferred data transfer protocol;
- -R – saves the time of the last modification of a deleted file;
- -s – minimize error information output;
- -S – displays error messages;
- -T – upload the file to the server;
- -v – output as much detail as possible;
- -y – minimize download speed;
- -Y – maximize download speed;
- -V – show version.
As you can see, the cURL syntax is case-sensitive, so be careful.
Examples of using cURL in PHP
The easiest thing to do with Libcurl is to generate POST requests. To do this you just need to initiate a cURL session, set the necessary options, and then execute the request. Here is a simple and clear example:
<?php // Searching for books on the amazon.com server $url = "http://www.amazon.com/exec/obidos/search-handle-form/002-5640957-2809605"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); // set url to post to curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s curl_setopt($ch, CURLOPT_POST, 1); // set POST method curl_setopt($ch, CURLOPT_POSTFIELDS, "url=index%3Dbooks&field-keywords=PHP+MYSQL"); // add POST fields $result = curl_exec($ch); // run the whole process curl_close($ch); echo $result; ?> <?php // HTTP authentication $url = "http://www.example.com/protected/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword"); $result = curl_exec($ch); curl_close($ch); echo $result; ?> <?PHP // FTP this script to a server $fp = fopen(__FILE__, "r"); $url = "ftp://username:password@mydomain.com:21/path/to/newfile.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_FTPASCII, 1); curl_setopt($ch, CURLOPT_INFILESIZE, filesize(__FILE__)); $result = curl_exec($ch); curl_close($ch); ?>
To better understand how cURL works, we recommend carefully examining the official documentation from the developers or asking experienced system administrators for clarification. This concludes our material and thank you for your attention!