PHP uses Curl to send get/post requests, upload images, batch

Source: Internet
Author: User

Curl is a tool that uses URL syntax to specify the transfer of files and data. PHP has curl extension, commonly used to achieve network crawling, analog send get POST request, file upload.

The basic steps to build curl in PHP are as follows:

1 Initialize the 2 settings option, including URL 3 execution and get result 4 release curl handle.

In work and study, I am also the usual curl. Since the various options are difficult to remember when using the Curl setting option, it is necessary to refer to some of the commonly used examples in this document for later reference.

Example one: Crawl Web data (take the open API as an example, also get request)

<?php header ("content-type:text/html; Charset=utf-8 ");  $ch = Curl_init ();//Initialize/*============ start setting curl various options ================*/curl_setopt ($ch, Curlopt_url, "http:// Open.lashou.com/opendeals/lashou/city.xml "); curl_setopt ($ch, Curlopt_returntransfer, 1); $html = Curl_exec ($ch);// Executes the handle, gets the return content curl_close ($ch);//release handle echo $html?>

If you use this method to send a GET request, the parameter is appended to the URL

such as curl_setopt ($ch, Curlopt_url, "http://localhost/tqj/date/p822.php?name=yyyyy");

Example 2 sending a POST request with Curl

<?php$uri = "http://localhost/tqj/date/p822.php";//Post parameter array $data = array (        ' name ' = ' Tianquanjun ',  ' Password ' + ' Tianquanjun ',);//Initialize $ch = Curl_init ();//Various item settings, on-line reference, you can view the PHP manual, set your own curl_setopt ($ch, Curlopt_url, $ URI); curl_setopt ($ch, Curlopt_post, 1);//post mode curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_retur Ntransfer, 1); curl_setopt ($ch, Curlopt_postfields, $data);//Execute $return = curl_exec ($ch);//Release curl_close ($ch); Print_r ($return);? >

  

Example three: Curl process debugging and error information processing

<?php$uri = "http://localhost/tqj/date/p822.php";//Post parameter array $data = array (        ' name ' = ' Tianquanjun ',  ' Password ' + ' Tianquanjun ',);//Initialize $ch = Curl_init ();//Various item settings, on-line reference, you can view the PHP manual, set your own curl_setopt ($ch, Curlopt_url, $ URI); curl_setopt ($ch, Curlopt_post, 1);//post mode curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_retur Ntransfer, 1); curl_setopt ($ch, Curlopt_postfields, $data);//Execute $return = curl_exec ($ch); //Fault tolerant mechanism if ($return = = = False) {Var_dump (Curl_error ($ch));} Curl_getinfo () gets various running information to facilitate debugging $info = Curl_getinfo ($ch); echo "Execution Time". $info [' Total_time ']. Php_eol; Release Curl_close ($ch); Print_r ($return);? >

where Curl_error () is used to obtain error information, Curl_getinfo () gets run-related information.

Example four upload a picture, get the return information. Upload images across domains and get back information, and this will work. Compared to post, note that the file is preceded by an @ symbol

<?php$uri = "http://localhost/tqj/date/p822.php";//Post parameter array $data = array (        ' author ' = ' Tianquanjun ',  ' Upload ' = ' @c:\users\tianquanjun. Dangdang\pictures\a.jpg ',);//Initialize $ch = Curl_init ();//Various item settings, online reference, you can view the PHP manual, your own settings curl_setopt ($ch, Curlopt_url, $uri) ; curl_setopt ($ch, Curlopt_post, 1);//post mode curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_returntran Sfer, 1); curl_setopt ($ch, Curlopt_postfields, $data);//Execute $return = curl_exec ($ch);//fault tolerant mechanism if ($return = = = False) {Var_d UMP (Curl_error ($ch));} Curl_getinfo () gets various running information to facilitate debugging $info = Curl_getinfo ($ch); echo "Execution Time". $info [' Total_time ']. php_eol;//release Curl_close ($ch); Print_r ($return);? >

 

Example five: Curl batch processing.

Curl has an advanced feature, batch handle. Allows multiple curl links to be opened.

Batching is to open multiple curl handles and assign those handles to a batch handle, and then wait in the while loop to finish. Curl_multi_exec () is called multithreading, but it still belongs to the category of Asynchrony.

<?php header ("content-type:text/html; CHARSET=GBK "); $urls =array (' http://www.baidu.com ', ' http://www.qq.com/'); $ch =array ();//batch handle $mh=curl_multi_init ();//Open Multiple Curl handles and assigned to a batch handle $ch[0]=curl_init ($urls [0]), $ch [1]=curl_init ($urls [1]), for ($i =0; $i <2; $i + +) {curl_setopt ($ch [$i], curlopt_returntransfer,1); Curl_multi_add_handle ($MH, $ch [$i]); } $running = null;do{    usleep (10000);    Curl_multi_exec ($MH, $running);//Implement batch processing, can be seen as curl Multi-threading, is actually asynchronous category}while ($running >0); $res =array (); for ($j =0; $j <2;$ J + +) {    $res [$j]=curl_multi_getcontent ($ch [$j]);} Close handle for ($k =0; $k <2; $k + +) {    curl_multi_remove_handle ($mh, $ch [$k]);} Curl_multi_close ($MH); Print_r ($res);    ? >

Basically, some examples are listed. To be flexible with curl, you have to be familiar with the various settings of curl, which are the soul of curl.

PHP uses Curl to send get/post requests, upload images, batch

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.