[Jie GE talk about PHP] 16th bullet-advanced use of file transfer tool cURL

Source: Internet
Author: User
Tags filetime
[Jie GE talk about PHP] 16th bullet --- advanced application of file transfer tool cURL. next we will talk about the above content. the above briefly introduces the four steps of using curl, this article will explain some advanced applications of curl: to obtain the request information, we can use curl_getinfo (): & lt ;? Php/[Jie GE talk about PHP] 16th bullet --- advanced application of file transfer tool cURL
Next, let's talk about the above content. the above section briefly introduces the four steps for using curl. This article will explain some advanced applications of curl:

Obtain the request information. after the curl is executed, use curl_getinfo ():
  1. // Create a new cURL resource
  2. $ Ch = curl_init ("http://www.lampbrother.net ");
  3. // Set the URL and corresponding options
  4. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  5. // Check for any errors
  6. If (! Curl_errno ($ ch ))
  7. {
  8. $ Info = curl_getinfo ($ ch );
  9. Var_dump ($ info );
  10. }
  11. // Capture the URL and pass it to the browser
  12. $ Html = curl_exec ($ ch );
  13. // Disable cURL resources and release system resources
  14. Curl_close ($ ch );
  15. ?>

    The printed content is:

    Array
    'URL' => string 'http: // www.lampbrother.net'(Length = 26)
    'Content _ type' => null
    'Http _ code' => int 0
    'Header _ size' => int 0
    'Request _ size' => int 0
    'Filetime' => int 0
    'SSL _ verify_result '=> int 0
    'Redirect _ count' => int 0
    'Total _ time' => float 0
    'Namelookup _ time' => float 0
    'Connect _ time' => float 0
    'Pretransfer _ time' => float 0
    'Size _ upload' => float 0
    'Size _ download' => float 0
    'Speed _ download' => float 0
    'Speed _ upload' => float 0
    'Download _ content_length '=> float-1
    'Upload _ content_length '=> float-1
    'Starttransfer _ time' => float 0
    'Redirect _ time' => float 0
    'Certinfo' =>Array
    Empty
    'Redirect _ url' => string''(Length = 0)

    The returned array contains the following information:
    "Url" // resource network address
    "Content_type" // content encoding
    "Http_code" // HTTP status code
    "Header_size" // header size
    "Request_size" // request size
    "Filetime" // file creation time
    "Ssl_verify_result" // SSL verification result
    "Redirect_count" // jump Technology
    "Total_time" // total time consumed
    "Namelookup_time" // DNS query time
    "Connect_time" // waiting for connection time
    "Pretransfer_time" // pre-transmission preparation time
    "Size_upload" // size of the uploaded data
    "Size_download" // size of the downloaded data
    "Speed_download" // download speed
    "Speed_upload" // upload speed
    "Download_content_length" // The length of the downloaded content
    "Upload_content_length" // length of the uploaded content
    "Starttransfer_time" // start time of transmission
    "Redirect_time" // time consumed by redirection

    We can even use curl to simulate the browser to send data in POST mode:

    Let's first create a page that can print POST data:
  1. Var_dump ($ _ POST );
  2. ?>
Create a new page to simulate a browser to send POST data:
  1. $ Url = "http: // localhost/post. php ";
  2. $ Post_data = array (
  3. "Author" => "Li Jie ",
  4. "Title" => "talking about PHP"
  5. );
  6. // Initialize and create a new cURL resource
  7. $ Ch = curl_init ();
  8. // Set the URL and corresponding options
  9. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  10. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  11. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  12. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
  13. // Capture the URL and pass it to the browser
  14. $ Out = curl_exec ($ ch );
  15. // Disable cURL resources and release system resources
  16. Curl_close ($ ch );
  17. Echo $ output;
  18. ?>
    The output is as follows:

    Array
    'Author' => string 'Jet Li'(Length = 4)
    'Title' => string' php'(Length = 11)


    We can see that the powerful curl has helped us pass the post data. it is like this process:

    1. pass post data to the post. php page
    2. the post. php page displays the post data output on the page.
    3. curl captures the post data received and printed by post. php and outputs the data on the page!

    We can not only use post to transmit data, but also Upload files in the same way:


    Curl. php
  1. $ Url = "http: // localhost/upload. php ";
  2. $ Post_data = array (
  3. "Title" => "amazing !!! ",
  4. "Pic" => "@ d: \ 文 .jpg"
  5. );
  6. // Initialize and create a new cURL resource
  7. $ Ch = curl_init ();
  8. // Set the URL and corresponding options
  9. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  10. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  11. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  12. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
  13. // Capture the URL and pass it to the browser
  14. $ Out = curl_exec ($ ch );
  15. // Disable cURL resources and release system resources
  16. Curl_close ($ ch );
  17. Echo $ output;
  18. ?>

Upload. php
  1. Var_dump ($ _ FILES );
  2. ?>

    Passed value:

    Array
    'Pic '=>Array
    'Name' => string 'Lee Venucia. JPG'(Length = 18)
    'Type' => string 'application/octet-stream'(Length = 24)
    'Tmp _ name' => string 'f: \ LAMPBrother \ Environmental \ wamp_32 \ tmp \ php9A73. tmp'(Length = 52)
    'Error' => int 0
    'Size' => int 0
  1. $ Post_data = array (
  2. "Title" => "amazing !!! ",
  3. "Pic" => "@ d: \ 文 .jpg"
  4. );

Note that the @ symbol must be added before the file name to be uploaded!

CURL batch processing:

CURL also has an advanced application, batch processing handle, which can process multiple URL connections synchronously or asynchronously:
  1. // Create a cURL resource
  2. $ Response = curl_init ();
  3. $ Ch2 = curl_init ();
  4. // Set the URL and corresponding options
  5. Curl_setopt ($ scheme, CURLOPT_URL, "http://www.li-jie.me /");
  6. Curl_setopt ($ scheme, CURLOPT_HEADER, 0 );
  7. Curl_setopt ($ ch2, CURLOPT_URL, "http://www.lampbrother.net /");
  8. Curl_setopt ($ ch2, CURLOPT_HEADER, 0 );
  9. // Create a cURL handle for batch processing
  10. $ Mh = curl_multi_init ();
  11. // Add two handles
  12. Curl_multi_add_handle ($ mh, $ handle );
  13. Curl_multi_add_handle ($ mh, $ ch2 );
  14. $ Running = null;
  15. // Execute the batch processing handle
  16. Do {
  17. Usleep (10000 );
  18. Curl_multi_exec ($ mh, $ running );
  19. } While ($ running> 0 );
  20. // Close all handles
  21. Curl_multi_remove_handle ($ mh, $ handle );
  22. Curl_multi_remove_handle ($ mh, $ ch2 );
  23. Curl_multi_close ($ mh );
  24. ?>

$ Running collects page content from http://www.li-jie.me and http://www.lampbrother.net for batch processing of multiple URLs!

As you can see, the collection website will discard file_get_contents and fopen in the future. using our powerful cURL will help you increase the color of your web applications!


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.