Curl Library's powerful features and flexible scalability

Source: Internet
Author: User
Tags curl error handling

    • Original: PHP based Curl Quick Start
    • English Original: Http://net.tutsplus.com/tutorial ... for-mastering-curl/
    • Original Author: Burak Guzel

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 CURL libraries. This article will introduce some of the advanced features of CURL and how to use it in PHP.

Why do you use CURL?

Yes, we can get the content of the webpage by other means. Most of the time, because I want to be lazy, I use simple PHP functions directly:

$content = file_get_contents ("http://www.nettuts.com");
Or
$lines = File ("http://www.nettuts.com");
Or
ReadFile (http://www.nettuts.com);

However, this approach lacks flexibility and effective error handling. Also, you can't use it to do some difficult tasks-such as processing coockies, validation, form submission, file uploads, and so on.

Reference:
CURL is a powerful library that supports many different protocols, options, and provides various details about URL requests.

Basic structure

Before you learn more complex features, take a look at the basic steps in creating a Curl request in PHP:

    1. Class
    2. Setting variables
    3. Execute and get results
    4. Release Curl handle

1. Class
$ch = Curl_init ();
2. Setting options, including URLs
curl_setopt ($ch, Curlopt_url, "http://www.nettuts.com");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 0);
3. Execute and get the contents of the HTML document
$output = curl_exec ($ch);
4. Release Curl handle
Curl_close ($ch);

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

Check for errors

You can add an error-checking statement (although this is not required):

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

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



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.