This article mainly introduces the introductory tutorial on using curl in PHP. This article explains the overview of curl, installation of curl, and the steps of using curl in PHP. A simple curl code example and other content, friends in need can refer to it
Overview
In my last article "Introduction to curl and libcurl", I briefly introduced curl-related knowledge to you. This article introduces you to the curl extension in PHP.
Although in the previous article, a distinction was made between curl and libcurl, and some related concepts were also explained. At the same time, I also learned that the curl extension in PHP is actually an encapsulation of libcurl. However, in this article, for the convenience of writing, these two concepts will no longer be distinguished. Therefore, the curl mentioned next in the article actually refers to libcurl. I hope it will not confuse everyone.
I won’t go into too much detail about the curl extension in PHP. You can check the documentation.
Install curl
Regarding the installation of curl, I won’t introduce too much here. The process is the same for windows and linux. Choose the appropriate installation method according to the platform, and then enable the curl extension in the php.ini file. The installation is the same as other extensions.
Steps to use curl in PHP
In PHP, you can use curl to complete various functions, such as crawling web pages, uploading/downloading files, simulated login, etc. However, the implementation of these functions is based on four steps, so the use of curl is not complicated.
When using curl, it is mainly divided into the following four steps:
1. Initialize a curl instance—curl_init()
2. Set related options when curl is executed—curl_setopt()
3. Execute curl query—curl_exec()
4. Close curl—curl_close()
Among these four steps, steps 1, 3, and 4 are all easy. The most troublesome step is step 2. This step is to set curl options. There are more than 100 different options. To complete different functions, these options must be combined.
The following is an explanation of these four steps:
1. Initialize a curl instance. This step uses the function curl_init(). Check the PHP manual. You can see that the return value of this function is a resource type. We need to use a variable to save this instance because This example will be used in subsequent steps. Specific code example:
The code is as follows:
$curl=curl_init(); //Output resource(2, curl)
2. Set curl related options. Use the function curl_setopt() to set curl options. This function accepts three parameters: the first parameter is the curl instance to be set, which is the instance in the first step. The second parameter is the option to be set, which is a predefined constant. What are the specific options? You can check it yourself in the manual. The third parameter is the specific value of the option to be set.
Code example:
The code is as follows:
curl_setopt ($curl, CURLOPT_URL, "http://www.php.net");
3. Execute curl query. This step uses the function curl_exec(). This function accepts one parameter, which is also the instance obtained in step 1.
Code example:
Copy the code. The code is as follows:
curl_exec ($curl);
4. Close the current curl. This step uses the function curl_close(). This function also accepts the curl instance obtained in step 1 as a parameter.
Code example:
The code is as follows:
curl_close($curl);
Using curl in PHP generally follows these four steps, of which different functions are mainly accomplished through different settings in the second step, so the second step is the most troublesome, and some even require careful understanding.
A simple curl code example
I have introduced you to the four steps of using curl before. Here I will give you a simple example of grabbing web content. The code is very simple, but I hope it can help you better understand curl.
Capture Baidu homepage content:
The code is as follows:
$curl=curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://www.baidu.com");
$baidu=curl_exec($curl);
curl_close($curl);
Run this code and the page will display the Baidu homepage.
Summary
As of today, I have written five or six blogs. I really want to record the knowledge I have learned, and I also want to share it with everyone, but I have always felt that my language organization ability is not very good. I don’t know if people who read the article can understand it. I hope that I can continue to improve in language organization in the future. Bar.