Real practical example: you need to reboot your router (modem) to change the IP address. To do this, you need to: log in to the router, go to the maintenance page and click the “Reboot” button. If this action needs to be performed several times, then the procedure must be repeated. Agree, you don’t want to do this routine manually every time. cURL allows you to automate all of this. With just a few cURL commands you can achieve authorization and complete the task on the router.
- cURL is useful for retrieving data from websites on the command line.
Those. cases of using cURL are quite real, although, in the majority, cURL is needed by programmers who use it for their programs.
CURL supports many protocols and authorization methods, can transfer files, works correctly with cookies, supports SSL certificates, proxies and much more.
cURL in PHP and command line
We can use cURL in two main ways: in PHP scripts and on the command line.
To enable cURL in PHP on the server, you need to uncomment the line in the php.ini file
Extension=php_curl.dll
And then reboot the server.
On Linux you need to install the curl package.
On Debian, Ubuntu or Linux Mint:
$ sudo apt-get install curl
On Fedora, CentOS or RHEL:
$ sudo yum install curl
To clearly see the difference in use in PHP and on the command line, we will perform the same tasks twice: first in the PHP script, and then on the command line. Let's try not to get confused.
Retrieving data using cURL
Retrieving data using cURL in PHP
Example in PHP:
Everything is very simple:
$target_url- the address of the site that interests us. After the site address, you can put a colon and add the port address (if the port is different from the standard one).
curl_init- initializes a new session and returns a handle, which in our example is assigned to a variable $ch.
We then execute the request with the cURL function curl_exec, to which a descriptor is passed as a parameter.
Everything is very logical, but when this script is executed, the contents of the site will be displayed on our page. But what if we don’t want to display the content, but want to write it to a variable (for subsequent processing or parsing).
Let's add a little to our script:
0) ( echo "Curl error: " . curl_error($ch); ) curl_close($ch); ?>
We have a line curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);.
curl_setopt- sets options. A complete list of options can be found on this page:
Hidden from guests
$response_data = curl_exec($ch);
Now the script value is assigned to the $response_data variable, with which further operations can be performed. For example, you can display its contents.
Stitches
If (curl_errno($ch) > 0) ( echo "Curl error: " . curl_error($ch); )
serve for debugging in case errors occur.
Retrieving data using cURL on the command line
On the command line, just type
where instead of mi-al.ru- the address of your website.
If you need to copy data into a variable rather than display the result on the screen, then do this:
Temp="curl mi-al.ru"
However, some data is still displayed:
To prevent them from being displayed, add the key -s:
Temp="curl -s mi-al.ru"
You can see what has been recorded:
Echo $temp | less
Basic and HTTP authenticationAuthentication, simply put, is entering a username and password.
Basic authentication is server-based authentication. For this, two files are created: .htaccess And .htpasswd
The contents of the .htaccess file are something like this
AuthName "For registered users only!" AuthType Basic require valid-user AuthUserFile /home/freeforum.biz/htdocs/.htpassw
The contents of the .htpasswd file are something like this:
Mial:CRdiI.ZrZQRRc
Those. login and password hash.
When you try to access a password-protected folder, the browser will display something like this:
HTTP authentication is the case when we enter a login and password into a form on a website. It is this authentication that is used when logging into mail, forums, etc.
Basic cURL authentication (PHP)
There is a website
Hidden from guests
Let's try our initial script:
0) ( echo "Curl error: " . curl_error($ch); ) else ( echo $response_data; ) curl_close($ch); ?>
Although the script believes that there is no error, we don’t like the output at all:
Add two lines:
Curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "ru-board:ru-board");
The first line we set the authentication type - basic. The second line contains the name and password separated by a colon (in our case, the name and password are the same - ru-board). It turned out like this:
0) ( echo "Curl error: " . curl_error($ch); ) else ( echo $response_data; ) curl_close($ch); ?> Let's try: 30946 Great! Basic cURL authentication (on the command line) The same can be achieved on the command line with one line: curl -u ru-board:ru-board http://62.113.208.29/Update_FED_DAYS/
I didn't forget to specify the authentication type, it's just that in cURL the basic authentication type is the default.
Everything worked out so quickly on the command line that, out of frustration, I wrote the following program. She connects to the site and downloads the latest update:
Temp=`curl -s -u ru-board:ru-board http://62.113.208.29/Update_FED_DAYS/ | grep -E -o "Update_FED_201(1).(2).(2).7z" | uniq | tail -n 1`; curl -o $temp -u ru-board:ru-board http://62.113.208.29/Update_FED_DAYS/$temp
With just a few more commands you can add:
- unpacking the archive into the specified directory;
- launching ConsultantPlus updates (these are updates for it);
- you can check whether the latest available update has already been downloaded or a new one has appeared;
- add it all to Cron for daily updates.
HTTP cURL authentication in PHP
We need to know:
- address where to send authentication data
- sending method GET or POST
- login
- password
The address where you need to send the data can be taken from the authentication form. For example: