"About PHP" 16th bomb-File Transfer tool Curl advanced use

Source: Internet
Author: User
Tags filetime
"About PHP" 16th bullet---File transfer tool Curl's advanced application
Let's move on to the above section, which gives you a brief introduction to the four steps of using curl, this article explains some of the advanced applications of Curl:

To obtain information about the request, we can take advantage of curl_getinfo () after Curl execution is complete:
  1. Create a new Curl resource
  2. $ch = Curl_init ("http://www.lampbrother.net");
  3. Set the URL and the appropriate options
  4. curl_setopt ($ch, curlopt_returntransfer,1);
  5. Check to see if an error has occurred
  6. if (!curl_errno ($ch))
  7. {
  8. $info = Curl_getinfo ($ch);
  9. Var_dump ($info);
  10. }
  11. Crawl the URL and pass it to the browser
  12. $html = curl_exec ($ch);
  13. Turn off the Curl resource and release the 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 '=Int0
    ' Header_size '=Int0
    ' Request_size '=Int0
    ' Filetime '=Int0
    ' Ssl_verify_result '=Int0
    ' Redirect_count '=Int0
    ' Total_time '=Float0
    ' Namelookup_time '=Float0
    ' Connect_time '=Float0
    ' Pretransfer_time '=Float0
    ' Size_upload '=Float0
    ' Size_download '=Float0
    ' Speed_download '=Float0
    ' Speed_upload '=Float0
    ' Download_content_length '=Float-1
    ' Upload_content_length '=Float-1
    ' Starttransfer_time '=Float0
    ' Redirect_time '=Float0
    ' Certinfo '=Array
    Empty
    ' Redirect_url '=String'' (length=0)

    The following information is included in the returned array:
    "url"//Resource Network address
    "Content_Type"//content encoding
    "Http_code"//http status code
    size of the "header_size"//header
    "Request_size"//Requested size
    "FILETIME"//File creation time
    "Ssl_verify_result"//ssl validation results
    "Redirect_count"//Jump technology
    "Total_time"//Total time-consuming
    "Namelookup_time"//dns query time-consuming
    "Connect_time"//Waiting for connection time
    "Pretransfer_time"//Pre-transmission preparation time
    "Size_upload"//size of uploaded data
    size of "size_download"//Download Data
    "speed_download"//download speed
    "Speed_upload"//upload speed
    "Download_content_length"//length of downloaded content
    "Upload_content_length"//Length of uploaded content
    "Starttransfer_time"//Start transfer time
    "Redirect_time"//redirect time-consuming

    we can even use curl to simulate the way the browser sends data by post:

    let's start by creating a page that can print the post data:

    1. Var_dump ($_post);
    2. ?>

Create a new page to simulate the browser sending post data:
  1. $url = "http://localhost/post.php";
  2. $post _data = Array (
  3. "Author" = "Jie Li",
  4. "title" and "Jie Brother about PHP"
  5. );
  6. Initialize, create a new curl resource
  7. $ch = Curl_init ();
  8. Set the URL and the appropriate 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. Crawl the URL and pass it to the browser
  14. $out = curl_exec ($ch);
  15. Turn off the Curl resource and release the system resources
  16. Curl_close ($ch);
  17. Echo $output;
  18. ?>
    Print out the results:

    Array
    ' Author '=String' Jie Li ' (length=4)
    ' Title '=String' talking about PHP ' in Czech Republic (length=11)


    we can see that the powerful curl has helped us pass the post data over, and it's a process like this:

    1. Pass the post data to the post.php page
    The 2.post.php page displays the post data output on the page
    3.curl post.php Receive and print out the post data crawl back, output on the page!

    not only can we use post to pass data, we can also upload files, essentially the same way:


    curl.php

    1. $url = "http://localhost/upload.php";
    2. $post _data = Array (
    3. "title" = "Amazing!!!" ",
    4. "Pic" = "@d:\ Li Wenkei only glamorous photos. jpg"
    5. );
    6. Initialize, create a new curl resource
    7. $ch = Curl_init ();
    8. Set the URL and the appropriate 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. Crawl the URL and pass it to the browser
    14. $out = curl_exec ($ch);
    15. Turn off the Curl resource and release the system resources
    16. Curl_close ($ch);
    17. Echo $output;
    18. ?>


upload.php

    1. !--? php
    2. var_dump ($_files);
    3. ?

      passed back value: Span style= "font-size:18px" >

      array
      ' Pic ' => array
      ' name ' => string Li Wenkei only glamorous photos. jpg ' (length=18)
      ' type ' (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:\ Li Wenkei only glamorous photos. jpg"
    4. );


the upload needs to be noted that [Email protected]!

Curl Batch processing:

Curl also has an advanced application, batch handle, which can handle multiple URL connections synchronously or asynchronously:
    1. Create a pair of curl resources
    2. $ch 1 = curl_init ();
    3. $ch 2 = Curl_init ();
    4. Set the URL and the appropriate options
    5. curl_setopt ($ch 1, Curlopt_url, "http://www.li-jie.me/");
    6. curl_setopt ($ch 1, curlopt_header, 0);
    7. curl_setopt ($ch 2, Curlopt_url, "http://www.lampbrother.net/");
    8. curl_setopt ($ch 2, Curlopt_header, 0);
    9. Create a batch curl handle
    10. $MH = Curl_multi_init ();
    11. Add 2 Handles
    12. Curl_multi_add_handle ($MH, $ch 1);
    13. Curl_multi_add_handle ($MH, $ch 2);
    14. $running =null;
    15. Executing a batch 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, $ch 1);
    22. Curl_multi_remove_handle ($MH, $ch 2);
    23. Curl_multi_close ($MH);
    24. ?>


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

You See, after the collection site to abandon file_get_contents and fopen bar, put our strong curl use up, will help your Web application to enhance a lot!


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