Introduction to Using Curl in PHP _php instance

Source: Internet
Author: User
Tags curl

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:

Copy Code code 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:

Copy Code code 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:
Copy Code code 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:

Copy Code code 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.