Detailed instructions on using curl in php

Source: Internet
Author: User
Curl is used for the work in the past two days. when the requested url is not on the same server, it cannot be accessed directly. in this case, it is best to use curl, simulate post requests to do something,

Curl is used for the work in the past two days. when the requested url is not on the same server, it cannot be accessed directly. in this case, it is best to use curl, it is simple and convenient to simulate post requests. The following describes the accumulation of post requests during usage. the instance code is as follows:

  1. /*
  2. Remote post request
  3. */
  4. Function getRemoteUrl ($ get_url ){
  5. $ Curl = curl_init (); # Start a CURL session
  6. Curl_setopt ($ curl, CURLOPT_URL, $ get_url); # set a Url
  7. Curl_setopt ($ curl, CURLOPT_POST, true); # send a regular Post request
  8. Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); # set the timeout limit to prevent endless loops
  9. Curl_setopt ($ curl, CURLOPT_HEADER, 0); # display the returned Header area content
  10. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); # The obtained information is returned as a file stream
  11. $ Return = curl_exec ($ curl); # perform the operation
  12. If (curl_errno ($ curl )){
  13. Return false;
  14. }
  15. Curl_close ($ curl); # Close a CURL session
  16. Return $ return;
  17. }
  18. Function getCurlData ($ url ){
  19. $ Curl = curl_init ();
  20. Curl_setopt ($ curl, CURLOPT_URL, $ url );
  21. Curl_setopt ($ curl, CURLOPT_HEADER, false );
  22. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
  23. Curl_setopt ($ curl, CURLOPT_TIMEOUT, 10 );
  24. $ Result = curl_exec ($ curl );
  25. Curl_close ($ curl );
  26. Return $ result;
  27. }

To sum up the curl method:

Initialize curl first, and use curl_setopt to set the target url and other options.

Curl_exec: execute curl

Disable curl

The last step is output.

Curl () is highly efficient and supports multiple threads. however, to enable curl extension, follow these steps:

1. copy the three php_curl.dll, libeay32.dll, and ssleay32.dll files in the PHP folder to system32;

2. remove the semicolon from php. ini (c: WINDOWS directory) and extension = php_curl.dll;

3. restart apache or IIS

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.