Some classic examples of curl

Source: Internet
Author: User

1. The basic steps of Curl Request:
(1) initialization

(2) Setting options, including URLs

(3) Execute and retrieve HTML document content

(4) Release curl handle

1<?PHP2     //1. Initialize3     $ch=curl_init ();4 5     //2. Set options, including URL6curl_setopt ($ch, Curlopt_url, "http://www.cnblogs.com/it-cen/");7 8     //returns the information obtained by CURL_EXEC () as a file stream, rather than directly outputting9curl_setopt ($ch, Curlopt_returntransfer, 1);Ten  One     //The header file information is output as a data stream at startup Acurl_setopt ($ch, Curlopt_header, 1); -  -     //3. Execute and get HTML document content theCurl_exec ($ch); -  -     //4. Release handle -Curl_close ($ch); +  -     Echo $ch; +  A?>

 Note: The second step is the most important, namely the curl_setopt () function

We can add a section to check the wrong statement, it is important to note that "===false" is used to distinguish between null output and Boolean value false

1 $output = curl_exec ($ch); 2 3     if ($outputfalse) {4         Echo "CURL Error:". Curl_error ($ch); 5     }

    The Curl_getinfo () function returns information related to this request after Curl executes, which is useful for debugging and troubleshooting:

1     Curl_exec ($ch); 2     $info = curl_getinfo ($ch); 3     Echo ' <pre> '; 4     Print_r ($info);     5     Echo

The returned data

Array (    [url] = http://www.cnblogs.com/it-cen/    [Content_Type] = text/html; Charset=utf-8    [Http_ Code] = [    header_size] = 312    [request_size] [    filetime] = 1 [    ssl_verify_result ] = 0    [Redirect_count] = 0    [Total_time] = 0.172    [Namelookup_time] = 0.016    [connect_ TIME] = 0.063    [Pretransfer_time] = 0.063    [size_upload] + 0    [size_download] = 14658 
   
    //Requested data size    [speed_download] = 85220    [speed_upload] + 0    [download_content_length] = 14658    [Upload_content_length] = 0    [Starttransfer_time] = 0.125    [Redirect_time] = 0    [ Certinfo] + Array        (        )    [redirect_url] = =)
   

2. This information is useful in debugging, for example, when curl crawls, it is possible for the network, and so on, often the incomplete crawl data, which we can calculate the filesize by the obtained data, and then Curl_getinfo () to obtain the comparison, if the size is equal, Make the download correct, or repeat the attempt.

let's look at an example of grabbing a picture:

1<?PHP2     Header("Content-type:image/png");3 4     //1. Initialize5     $ch=curl_init ();6 7     //2. Set options, including URL8curl_setopt ($ch,, Curlopt_url, "Http://img04.taobaocdn.com/tfscom/TB1omaTHXXXXXajXVXXtKXbFXXX.png");9curl_setopt ($ch, Curlopt_returntransfer, 1);Tencurl_setopt ($ch, Curlopt_header, 1); One  A     //3. Execute and get content -     $res= Curl_exec ($ch); -  the     //Get information -     $info= Curl_getinfo ($ch); -  -     //4. Releasing Resources +Curl_close ($ch); -  +     file_put_contents("D:/aa.png",$res); A  at     $size=filesize("D:/aa.png"); -  -     if($size!=$info[' Size_download ']) { -         Echo"The downloaded data is incomplete, please re-download"; -}Else { -         Echo"Download Data Complete"; in     } -  to  +?>

3. Send data in curl using the Post method

1<?PHP2     $ch=curl_init ();3 4     $data=Array(' name ' = ' Kelly ', ' Age ' =>27, ' sex ' =>1);5 6curl_setopt ($ch, Curlopt_url, "http://localhost.post.php");7curl_setopt ($ch, Curlopt_returntransfer, 1);8 9     //set to postTencurl_setopt ($ch, Curlopt_post, 1); One     //Add the post variable to the Acurl_setopt ($ch, Curlopt_postfields,$data); -  -     $res= Curl_exec ($ch); the  -Curl_close ($ch); -  -     Echo $res; +  -  +?>

This method can be simulated message, or can sit irrigation robot, the idea is the same

4. Uploading Files with Curl

1<?PHP2     //request the uploaded data3     $data=Array(' name ' = ' Beauty ', ' upload ' = ' @a.zip ');4 5     $ch=curl_init ();6 7curl_setopt ($ch, Curlopt_url, "http://127.0.0.1/Socket/upload_file.php");8curl_setopt ($ch, Curlopt_returntransfer, 1);9 Tencurl_setopt ($ch, Curlopt_post, 1); Onecurl_setopt ($ch, Curlopt_postfields,$data); A  -     $res= Curl_exec ($ch); -  theCurl_close ($ch); -  -     Echo $res; -  +?>

Note: To send a file, precede the filename with the @ prefix and use the full path

5. Curl Settings

In fact, curl has a lot of configuration options, these options are the soul of curl, through the setopt () settings, the following summarizes a few of the more common and important configuration items, I hope that the reader in the later use of curl will have some help:

Curlopt_autoreferer: Automatically set referer in header when based on location: Redirect: info

Curlopt_cookiesession: When enabled, curl tightly passes a sessioncookie, ignoring other cookies

Curlopt_header: outputting header file information as a data stream

Curlopt_infilesize: Set the size of the upload file in bytes

Curlopt_maxconnects: Maximum number of connections allowed

Curlopt_maxredirs: Specify the maximum number of HTTP redirects

Curlopt_cookie: Set the contents of the "COOKIE:" section of the HTTP request, multiple cookies with a semicolon followed by a semicolon followed by a space

Curlopt_postfields: All data is sent with the "POST" action in the HTTP protocol to send the file, precede the file name with the @ prefix and use the full path

.......

Please refer to the PHP manual for more specific configuration items.

Curl is very powerful, it is a common library, not PHP exclusive.

I hope readers can learn something from some of the classic curl examples in this blog post.

Some classic examples of curl

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.