Getting started with curl in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags curl options how to use curl
Getting started with curl in PHP. Getting started with curl in PHP this article mainly introduces the Getting Started with curl in PHP, this article explains curl overview, installation of curl, steps for using curl in PHP, and a simple curl code implementation tutorial on using curl in PHP

This article describes how to use curl in PHP. This article describes curl overview, how to install curl, how to use curl in PHP, and a simple curl code instance, for more information, see

Overview

In my previous article "introduction to curl and libcurl", I briefly introduced the knowledge of curl. This article introduces the curl extension in PHP.

Although in the previous article, curl and libcurl are differentiated and some related concepts are also explained. At the same time, we also know that the curl extension in PHP is actually an encapsulation of libcurl. However, in this article, we will not differentiate the two concepts for ease of writing. Therefore, the curl mentioned next in this article actually refers to libcurl, hoping it will not confuse everyone.

The curl extension in PHP is not described too much here. you can check the documentation.

Install curl

The installation of curl is not described too much here. Windows and linux are the same process. select the appropriate installation method based on the platform, and enable curl extension in the php. ini file, which is the same as other extensions.

Steps for using curl in PHP

In PHP, you can use curl to complete a variety of functions, such as crawling webpages, uploading/downloading files, and simulating logon. However, the implementation of these functions is based on four steps, so the use of curl is not complex.

When using curl, you can perform the following four steps:

1. initialize a curl instance-curl_init ()

2. set the curl execution option-curl_setopt ()

3. execute curl query-curl_exec ()

4. close curl-curl_close ()

Steps 1, 3, and 4 are easy. The most troublesome step is step 2. here, we set curl options. there are more than 100 different options. to complete different functions, we need to combine these options.

The following describes the four steps:

1. initialize a curl instance. in this step, use the curl_init () function. check the PHP manual and you can see that the returned value of this function is a resource type, we need to use a variable to save this instance, because this instance will be used in subsequent steps. Sample code:

The code is as follows:

$ Curl = curl_init (); // output resource (2, curl)

2. set curl-related options and use the curl_setopt () function to set curl options (). This function accepts three parameters: the first parameter is the curl instance to be set, that is, the instance in step 1, and the second parameter is a predefined constant, you can refer to the manual for specific options. The third parameter is the specific value of the option to be set.

Sample code:

The code is as follows:

Curl_setopt ($ curl, CURLOPT_URL, "http://www.php.net ");

3. execute the curl query. in this step, use the curl_exec () function (). This function accepts a parameter, which is also an instance obtained in step 1.

Sample code:

The code is as follows:

Curl_exec ($ curl );

4. close the current curl. in this step, use the curl_close () function (). This function also accepts the curl instance obtained in step 1 as the parameter.

Sample code:

The code is as follows:

Curl_close ($ curl );

The use of curl in PHP generally follows these four steps, in which different functions are implemented through different settings in step 2, so step 2 is the most troublesome, some may even need your understanding.

A simple curl code instance

We have introduced the four steps for using curl. here we will give you a simple demonstration of an instance for capturing web content. the code is very simple, but I hope it will 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. The Baidu homepage is displayed.

Summary

As of today, I have written five or six blogs. I really want to record my learning knowledge and share it with you. but I always feel that my language organization skills are not very good. I don't know if anyone who can understand the article can understand it, I hope we can continue to improve our language organization in the future.

This article describes how to use curl in PHP. This article describes curl overview, how to install curl, how to use curl in PHP, a simple curl code implementation...

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.