CURL is an extension that sends GET and POST data to a remote request. CURL is an extension that sends GET and POST data to a remote request. CURL is an extension that sends GET and POST data to a remote request. Such as file uploads and multiple requests sent simultaneously. This is something I have done in my actual work. CURL is not as popular as direct POST requests, but it is also very popular.

File upload

The first is the file upload function, PHP5.5 before and after the syntax is not the same, here of course we are the latest way to demonstrate the implementation of file upload function.

$file = "./1. learn about a PHP extension for detecting dangerous functions taint.php";

$ch = curl_init("http://localhost:9001");
$cfile = new CURLFile($file.'text/plain'.'phpfile.php');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1.// CURLOPT_POSTFIELDS => ['a' =>' POST test ', 'files' => curl_file_create($file, 'text/plain', 'phpfile.php')],
    CURLOPT_POSTFIELDS => ['a'= >'post'.'files'= >$cfile]]);$res = curl_exec($ch);

curl_close($ch);

var_dump($res);
// string(244) "Test data
/ / post test
// Array
/ / (
// [files] => Array
/ / (
// [name] => phpfile.php
// [type] => text/plain
// [tmp_name] => /private/tmp/phpvKyesy
// [error] => 0
// [size] => 1785
/ /)

// )
// "
Copy the code

The example is very simple and is the most basic implementation of file uploading. All we need is a CURLFile object in the POST field to upload the file. It takes three parameters to instantiate: the path of the file to upload, the MIME type, and the file name to display when uploading. Regarding the file name for this upload, we can see that when $_FIELS is printed on the server side, it shows up in the name field. For the most common image files, the MIME type is something like image/ JPEG, while for text files, text/plain will do, like the PHP files we uploaded directly in this test code.

Instead of using the CURLFile object for instantiation, we can use the curl_file_create() function directly, which also returns a CURLFile object, which is essentially a procedural-oriented version of the CURLFile object instantiation. The actions and parameters are the same.

Batch CURL Request

This function was used by me in the advertising system before. For the sent advertising articles, baidu will use this batch request to check whether the multiple links contain our advertising data. Of course, it is not executed concurrently, but requested sequentially.

$ch1 = curl_init();
$ch2 = curl_init();

curl_setopt($ch1, CURLOPT_URL, "https://www.baidu.com/");

curl_setopt($ch2, CURLOPT_URL, "http://localhost:9001");
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, ['a'= >'post']);

$mh = curl_multi_init();
curl_multi_add_handle($mh.$ch1);
curl_multi_add_handle($mh.$ch2);

$running=null;
do {
    sleep(2);
    curl_multi_exec($mh.$running);
    echo "= = = = = = = = = = = = =".$running."= = = = = = = = = = = = =", PHP_EOL;
} while ($running > 0);

curl_multi_remove_handle($mh.$ch1);
curl_multi_remove_handle($mh.$ch2);
curl_multi_close($mh);

/ / = = = = = = = = = = = = = 2 = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = 2 = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = 2 = = = = = = = = = = = = =
// Test data
/ / post test
/ / = = = = = = = = = = = = = 2 = = = = = = = = = = = = =
/ / = = = = = = = = = = = = = 1 = = = = = = = = = = = = =
/ / <! DOCTYPE html><! --STATUS OK-->
// <html>
// <head>
// 	<meta http-equiv="content-type" content="text/html;charset=utf-8">
// 
      
// 
      
// 
      
// 
      
// 
      
// 
      
// 
      
// 
      
// 
      
 
/ /...............................................................
/ /...............................................................
/ /...............................................................
// </body></html>
/ / = = = = = = = = = = = = = = = = = = = = = = = = = =
Copy the code

Get a batch CURL handle from curl_multi_init(), then add a normal CURL handle from curl_multi_add_handle(). At execution time, requests are executed in the order they were added. Each request can set its own options, such as GET and POST for different requests.

During execution, you can see that the later-added request is executed first. The second parameter in curl_multi_exec() is a reference parameter that continuously returns the currently executed task, decreasing from the second task until there are no more tasks, which we judge as running > 0.

conclusion

CURL also has some functionality, such as curl_share_init(), which is used infrequently, but can be used to share data between different request handles, such as cookies. If you need it, you can refer to the relevant documents. CURL is used frequently, and the Settings of opt constants are more important. In addition to the frequently used ones, most of the time, you need to look up the content you need in the documents. After all, the cost of memory is too high.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/r…