Introductory tutorial on using curl in PHP, phpcurl introductory tutorial_PHP tutorial

WBOY
Release: 2016-07-13 09:47:29
Original
940 people have browsed it

Introductory tutorial on using curl in PHP, introductory tutorial on phpcurl

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 introduce too much about the curl extension in PHP here. 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()

Of 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.
Below 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:
Copy code 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:
Copy code 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 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:
Copy code 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 introduced you to the four steps of using curl earlier. Here I will briefly demonstrate an example of grabbing web content. The code is very simple, but I hope it can help you better understand curl.
Capture Baidu homepage content:
Copy code 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1026060.htmlTechArticleIntroductory tutorial on using curl in PHP. An overview of the introductory tutorial on phpcurl is in my previous article "Introduction to curl and libcurl" In this article, I briefly introduce curl-related knowledge to you. This article introduces to you...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template