Use cURL in php to send get/post requests, upload images, and Batch Processing

Source: Internet
Author: User
CURL is a tool that uses the url syntax to specify file and data transmission. Php has curl extension, which is generally used for network crawling, simulating getpost requests and file uploads. The basic steps for creating a curl in php are as follows: 1. initialize 2. Set options, including url3 execution and obtain result 4. Release the curl handle. I often use it at work and study.

CURL is a tool that uses the url syntax to specify file and data transmission. Php has curl extension, which is generally used for network crawling, simulating the sending of get post requests and file uploads. The basic steps for creating a curl in php are as follows: 1 initialize 2 Settings options, including url 3 Execution and Result 4 release the curl handle. I often use it at work and study.

CURL is a tool that uses the url syntax to specify file and data transmission. Php has curl extension, which is generally used for network crawling, simulating the sending of get post requests and file uploads.

Follow these steps to create a curl in php:

1. initialize option 2, including url 3 Execution and obtain result 4. Release the curl handle.

In my work and study, I also often use curl. Since the curl setting options are hard to remember and need to be referenced, some common examples are recorded here for later reference.

Example 1: Capture web page data (take the api opened by the developer network as an example, and it is also a get request)

 

If you use this method to send a get request, you can append the parameter to the url.

For example, curl_setopt ($ ch, CURLOPT_URL, "http: // localhost/tqj/date/p822.php? Name = yyyyy ");

Instance 2 uses curl to send a post request

 'Tianjun', 'Password' => 'tianquanjun',); // initialize $ ch = curl_init (); // various settings. For more information, see the php manual, set curl_setopt ($ ch, CURLOPT_URL, $ uri); curl_setopt ($ ch, CURLOPT_POST, 1); // curl_setopt ($ ch, CURLOPT_HEADER, 0) in post mode ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // execute $ return = curl_exec ($ ch ); // release curl_close ($ ch); print_r ($ return);?>

  

Example 3: curl process debugging and Error Message Processing

 'Tianjun', 'Password' => 'tianquanjun',); // initialize $ ch = curl_init (); // various settings. For more information, see the php manual, set curl_setopt ($ ch, CURLOPT_URL, $ uri); curl_setopt ($ ch, CURLOPT_POST, 1); // curl_setopt ($ ch, CURLOPT_HEADER, 0) in post mode ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // execute $ return = curl_exec ($ ch ); // fault tolerance mechanism if ($ return = false) {var_dump (curl_error ($ ch )) ;} // Curl_getinfo () to obtain various running information, facilitating debugging $ info = curl_getinfo ($ ch); echo "execution time ". $ info ['total _ time']. PHP_EOL; // release curl_close ($ ch); print_r ($ return);?>

Use curl_error () to obtain the error information, and use curl_getinfo () to obtain the running information.

Instance 4 uploads an image to obtain the returned information. This allows you to upload images across domains and obtain the returned information. It is similar to post. Note that a @ symbol is added before the file.

 'Tianjun', 'upload' => '@ C: \ Users \ tianquanjun. DANGDANG \ Pictures \ a.jpg ',); // initialize $ ch = curl_init (); // various item settings. For more information, see the php manual, set curl_setopt ($ ch, CURLOPT_URL, $ uri); curl_setopt ($ ch, CURLOPT_POST, 1); // curl_setopt ($ ch, CURLOPT_HEADER, 0) in post mode ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // execute $ return = curl_exec ($ ch ); // fault tolerance mechanism if ($ return = False) {var_dump (curl_error ($ ch);} // curl_getinfo () to obtain various running information, facilitating debugging $ info = curl_getinfo ($ ch ); echo "execution time ". $ info ['total _ time']. PHP_EOL; // release curl_close ($ ch); print_r ($ return);?>

 

Example 5: curl batch processing.

Curl has an advanced feature, batch processing handle. Multiple curl links can be opened.

Batch Processing is to open multiple curl handles, assign these handles to a batch processing handle, and then wait for the processing to complete in the while loop. Curl_multi_exec () is called multi-thread processing, but it is still asynchronous.

 0); $ res = array (); for ($ j = 0; $ j <2; $ j ++) {$ res [$ j] = curl_multi_getcontent ($ ch [$ j]);} // close the handle for ($ k = 0; $ k <2; $ k ++) {curl_multi_remove_handle ($ mh, $ ch [$ k]);} curl_multi_close ($ mh); print_r ($ res);?>

Some common instances are listed. To use curl flexibly, you must be familiar with the configuration items of curl. these settings are the soul of curl.

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.