This article mainly introduces PHP to use Curl Introductory tutorial, this article explained Curl overview, installation curl, PHP use curl steps, a simple curl code instance and so on, need friends can refer to the following
Overview
In my last article "Curl and Libcurl Introduction" simple to introduce curl related knowledge. This article introduces you to the Curl extension in PHP.
Although the curl and Libcurl were distinguished in the previous article, some related concepts were also explained. At the same time, also know that the curl extension in PHP is actually the Libcurl encapsulation. However, in this article, in order to write easily, will no longer distinguish between these two concepts, so the article next mentioned Curl actually refers to Libcurl, hope will not confuse everyone.
About PHP Curl Extension here is not too much to introduce, you can check the file below.
Install Curl
About the installation of curl, there is also no too much introduction. Windows and Linux are all the same processes, depending on the platform to choose the appropriate installation method, and then open the curl extension in the php.ini file, and other extensions are the same installation.
Steps to use curl in PHP
In PHP, you can use curl to complete a variety of functions, such as crawling Web pages, file upload/download, analog login, and so on. 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. Initialization of a Curl instance-curl_init ()
2. Set the relevant options for Curl execution-curl_setopt ()
3. Execute Curl Query-curl_exec ()
4. Close Curl-curl_close ()
Of these four steps, 1, 3, 4 steps are easy. The most troublesome is 2 steps, this step to set the Curl option, here are more than 100 different options, to complete different functions, these options will be combined.
Here are four steps to illustrate:
1. Initialize a Curl instance, this step using the function Curl_init (), check the PHP manual, you can see that the return value of the function is a resource (Resource) type, we need to use a variable to save this instance, because the next step will use this instance. Specific code example:
The code is as follows:
$curl =curl_init (); Output resource (2, Curl)
2. Set Curl related options, set the curl option to use function curl_setopt (). The function accepts three parameters: the first parameter is the instance of the curl to set, which is the instance in the first step, the option to set the second parameter, which is a predefined constant, and what are the options, which you can refer to in the manual. The third parameter is the specific value of the option you want to set.
code example:
The code is as follows:
curl_setopt ($curl, Curlopt_url, "http://www.php.net");
3. Execute Curl Query, this step uses function Curl_exec (). The function accepts a parameter, which is also the instance obtained in step 1th.
code example:
Copy code code as follows:
Curl_exec ($curl);
4. Close the current curl, this step uses the function Curl_close (). The function also accepts the Curl instance obtained in step 1th as an argument.
code example:
The code is as follows:
Curl_close ($curl);
In PHP use curl generally follow these four steps, which is mainly through the 2 steps of different settings to complete different functions, so the 2nd step is the most troublesome, and some even need to understand attentively.
A simple example of Curl code
Before we introduced the use of Curl four steps, here to give you a simple demonstration of crawling Web content instances, the code is very simple, but hope to help you better understand curl.
Crawl 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 section of code, the page will display Baidu homepage.
Summarize
As of today, five or six blog posts have been written. Very want to learn the knowledge of their own record down, but also want to share with you, but always feel that their language organization ability is not too good, do not know to see the article people can understand, hope that in the future in the language organization can continue to progress.