Getting Started with curl in PHP tutorial _php tutorial

Source: Internet
Author: User
Tags curl options

Getting Started with curl in PHP tutorial


This article mainly introduces the use of curl in PHP tutorial, this article explains Curl overview, installation curl, PHP using curl, a simple example of curl code, and so on, the need for friends to refer to the following

Overview

In my previous article, "Curl and Libcurl Introduction", we simply introduced curl-related knowledge. This article introduces you to the Curl extension in PHP.

Although in the previous article, we made a distinction between curl and libcurl, some related concepts were explained. At the same time, I also know that the curl extension in PHP is actually the encapsulation of Libcurl. However, in this article, in order to write convenient, will no longer distinguish between the two concepts, so the article next mentioned Curl actually refers to libcurl, hope not to confuse everyone.

About the Curl extension in PHP here is not too much to introduce, you can check the following document.

Installing Curl

About the installation of curl, there is no too much introduction here. Windows and Linux are the same process, depending on the platform to choose the appropriate installation method, and then open the curl extension in the php.ini file, and the installation of other extensions are the same.

Steps for using curl in PHP

In PHP, you can use curl to perform a variety of functions, such as crawling Web pages, uploading/downloading files, simulating logins, and more. However, the implementation of these features is based on four steps, so the use of curl is not complicated.

When using curl, it is divided into the following four steps:

1. Initialize an instance of Curl-curl_init ()

2. Set the relevant options for Curl execution-curl_setopt ()

3. Perform a Curl query-curl_exec ()

4. Close Curl-curl_close ()

In these four steps, 1, 3, and 4 steps are easy. The most troublesome is 2 steps, this step to set the curl options, there are more than 100 different options, to complete the different functions, it is necessary to combine these options.

Here's a description of these four steps:

1. Initialize a Curl instance, this step uses the function Curl_init (), look at the PHP manual, you can see that the function's return value is a resource (Resource) type, we need to use a variable to save this instance, because the subsequent steps will use this instance. Specific code examples:

The code is as follows:

$curl =curl_init (); Output resource (2, Curl)

2. Set Curl-related options, set curl option to use function curl_setopt (). The function accepts three parameters: the first parameter is the instance of Curl to be set, which is the instance in the first step, the option to set for the second parameter, which is a predefined constant, and what options are available for you to check in the manual. The third parameter is the specific value of the option to set.

code example:

The code is as follows:

curl_setopt ($curl, Curlopt_url, "http://www.php.net");

3. Perform a curl query, this step uses the function curl_exec (). The function accepts a parameter, which is also the instance obtained in the 1th step.

code example:

Copy the 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 a parameter.

code example:

The code is as follows:

Curl_close ($curl);

The use of curl in PHP generally follow these four steps, which is mainly through the different settings of 2 steps to complete different functions, so the 2nd step is the most troublesome, and some even need to understand the intentions of people.

A simple example of Curl code

Before you introduce the use of Curl four steps, here to give you a simple walkthrough of crawling Web content instance, 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 show Baidu homepage.

Summarize

As of today, five or six blog posts have been written. Very want to learn the knowledge of their own records, but also want to share, but always feel that their language organization ability is not too good, do not know to see the article can be read, hope that in the language organization can continue to improve it.

http://www.bkjia.com/PHPjc/1025894.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025894.html techarticle Getting started with curl in PHP This article focuses on using curl in PHP to get started tutorial, this article explains Curl overview, installing curl, the steps to use curl in PHP, a simple Curl code real ...

  • 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.