PHP-based Curl QuickStart (i)

Source: Internet
Author: User
Curl is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, and so on. Best of all, PHP also supports the CURL library. This article describes some of the advanced features of CURL and how to use it in PHP.

Why do you use CURL?

Yes, we can obtain the content of the webpage by other means. Most of the time, I'm just using simple PHP functions because I want to be lazy:

$content = file_get_contents ("http://www.aezo.cn");//Or$lines = File ("http://www.aezo.cn"); Orreadfile ("http://www.aezo.cn");

However, this approach lacks flexibility and effective error handling. And you can't use it to do some difficult tasks-such as handling coockies, validating, form submissions, file uploads, and more.

Basic structure

Before learning more complex features, take a look at the basic steps to build a curl request in PHP:

    1. Initialization
    2. Setting variables
    3. Execute and get results
    4. Releasing the curl handle

1. Initialize $ch = Curl_init ();//2. Settings options, including Urlcurl_setopt ($ch, Curlopt_url, "http://www.aezo.cn") curl_setopt ($ch, Curlopt_returntransfer, 1); Curl_ Setopt ($ch, Curlopt_header, 0);//3. Executes and obtains the contents of the HTML document $output = curl_exec ($ch);//4. Release Curl handle Curl_close ($ch);

The second step (that is, curl_setopt ()) is the most important, and all mysticism is here. There is a long list of curl parameters that can be set to specify the details of the URL request. It may be difficult to read them all at once and to understand them, so today we'll just try some of the more commonly used and more useful options.

Check for errors

You can add a section that checks for errors (although this is not required):

... $output = curl_exec ($ch); if ($output = = = FALSE) {    echo "curl Error:". Curl_error ($ch);} // ...

Please note that we use "= = = False" instead of "= = false" when comparing. Because we have to distinguish between the null output and the Boolean value false, the latter is the real error.

The above describes the PHP-based Curl QuickStart (a), including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

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