PHP curl in php

Source: Internet
Author: User

Through1. Default call method of php curl, access url in get Mode

 
 
  1. ....
  2. $Ch=Curl_init();
  3. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // sets the http header
  4. Curl_setopt ($ ch, CURLOPT_ENCODING, "gzip ");
    // Set the client to support gzip Compression
  5. Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 30 );
    // Set the connection wait time
  6. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  7. Curl_exec ($ ch );
  8. If ($Error=Curl_error($ Ch )){
  9. // Handle errors
  10. Return-1;
  11. }
  12. Fclose ($ fp );
  13. $Curl_code=Curl_getinfo($ Ch, CURLINFO_HTTP_CODE );
    // Obtain the http Return Value
  14. If ($Curl_code= 200 ){
  15. // Normal access url
  16. }
  17. // Exception
  18. ....

2. Set http header to support php curl access to the lighttpd Server

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

3. Set curl to get only the http header and not the body:

 
 
  1. curl_setopt($ch, CURLOPT_HEADER, 1);       
  2. curl_setopt($ch, CURLOPT_NOBODY, 1);      

Or you can only get the 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 VM, you must set the Host

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

5. Use post, put, delete and other REStful access URLs

 
 
  1. Post:
  2.  
  3. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  4.  
  5. Put, delete:
  6.  
  7. Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "DELETE ");
    // Or PUT, which must be supported by the server.

6. php curl saves the downloaded content as a file

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

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.