PHP Curl application in PHP language _php tutorial

Source: Internet
Author: User
We pass on1. Default calling method for PHP curl, get access URL

 
 
  1. ....
  2. $ CH = Curl_init ();
  3. curl_setopt ($ch, Curlopt_httpheader, $header);//Set HTTP header
  4. curl_setopt ($ch, curlopt_encoding, "gzip");
    Set to support gzip compression for clients
  5. curl_setopt ($ch, Curlopt_connecttimeout, 30);
    Setting the connection Wait time
  6. curl_setopt ($ch, Curlopt_url, $url);
  7. Curl_exec ($ch);
  8. if ($error = curl_error($ch)) {
  9. Error handling
  10. return-1;
  11. }
  12. Fclose ($FP);
  13. $ Curl_code = Curl_getinfo ($ch, Curlinfo_http_code);
    Get the HTTP return value
  14. if ($curl_code = =) {
  15. Normal Access URL
  16. }
  17. Abnormal
  18. ....

2. Set HTTP header to support PHP Curl Access LIGHTTPD Server

 
  
  
  1. $header []= ' Expect: ';

3. Set curl to get the HTTP header only, without getting the body:

 
  
  
  1. curl_setopt ($ch, Curlopt_header, 1);
  2. curl_setopt ($ch, curlopt_nobody, 1);

Or just get body:

 
  
  
  1. curl_setopt ($ch, Curlopt_header, 0);
    Make sure we get the body
  2. curl_setopt ($ch, curlopt_nobody, 0);

4. To access the virtual host, you need to set the host

 
  
  
  1. $header []= ' Host: '. $host;

5. Use a restful way to access the URL using post, put, delete, etc.

 
  
  
  1. Post
  2. curl_setopt ($ch, Curlopt_post, 1);
  3. Put, delete:
  4. curl_setopt ($ch, Curlopt_customrequest, "DELETE");
    Or put, you need the server to support these methods.

6. PHP Curl saves the downloaded content as a file

 
  
  
  1. curl_setopt ($ch, Curlopt_file, $fp);

http://www.bkjia.com/PHPjc/446438.html www.bkjia.com true http://www.bkjia.com/PHPjc/446438.html techarticle we pass the default call method to 1. PHP curl, get way access URL .... $ ch = curl_init (); curl_setopt ($ch, Curlopt_httpheader, $header);//Set HTTP header C Url_setopt ($ch, Curlopt_ ...

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