Using CURL to send get and post request upload picture batch processing function in PHP

  • 2021-11-10 08:59:16
  • OfStack

cURL is a tool that uses url syntax to specify the transfer of files and data. There are curl extensions in php, which are used to realize network crawling, simulate sending get post requests and upload files.

The basic steps to establish curl in php are as follows:

Step 1 Initialize

2. Set options, including url

3. Execute and get results

4. Release the curl handle.

In my work and study, I am also commonly used in curl. Because when using curl to set options, various options are difficult to remember and need to be referred to, so a few examples commonly used are recorded here for later reference.

Example 1: Crawling web page data (take the open api of Handle Network as an example, which is also an get request)


<?php
header("Content-type: text/html; charset=utf-8"); 
$ch = curl_init();// Initialization 
/*============ 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);// Execute the handle to get the returned content 
curl_close($ch);// Release handle 
echo $html

If get is requested in this way, the parameters can be appended to url, such as curl_setopt($ch, CURLOPT_URL, "http://localhost/tqj/date/p822.php?name=yyyyy");

Example 2: Sending an post request using curl


<?php
$uri = "http://localhost/tqj/date/p822.php";
// post Array of parameters 
$data = array (
  'name' => 'tianquanjun',
  'password' => 'tianquanjun',
);
// Initialization 
$ch = curl_init ();
// Various item settings, online reference, can be viewed php Manual, set up by yourself 
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post Mode 
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
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 Handling


<?php
$uri = "http://localhost/tqj/date/p822.php";
// post Array of parameters 
$data = array (
  'name' => 'tianquanjun',
  'password' => 'tianquanjun',
);
// Initialization 
$ch = curl_init ();
// Various item settings, online reference, can be viewed php Manual, set up by yourself 
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post Mode 
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 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() Get all kinds of running information for debugging  
$info = curl_getinfo($ch);
echo " Execution time ".$info['total_time'].PHP_EOL;
// Release 
curl_close ( $ch );
print_r($return);
?>

Which utilizes curl_error() Get the error message, curl_getinfo() Get information about running.

Example 4: Upload pictures and get the return information.

Upload pictures across domains and get the return information at the same time, which can show your talents. It is similar to post. Note that the file is preceded by a @ symbol


<?php
$uri = "http://localhost/tqj/date/p822.php";
// post Array of parameters 
$data = array (
  'author' => 'tianquanjun',
  'upload' => '@C:\Users\tianquanjun.DANGDANG\Pictures\a.jpg',
);
// Initialization 
$ch = curl_init ();
// Various item settings, online reference, can be viewed php Manual, set up by yourself 
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post Mode 
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 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() Get all kinds of running information for 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 handles. Allows multiple curl links to open.  

Batch is to open multiple curl handles, assign them to one batch handle, and wait in an while loop to finish processing. curl_multi_exec () is multithreaded, but it is still asynchronous.


<?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 Handle and assigned to the 1 Batch handles 
$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, which can be seen as curl Multithreading is actually an 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, one common example is listed. If you want to use curl flexibly, you still need to be familiar with the various settings of curl, which are the soul of curl.

Summarize


Related articles: