Initialization
- $ch = Curl_init ();
To upload the local file address "@f:/xampp/php/php.ini" upload time, the upload path must be preceded by the @ symbol
- $furl = "@f:/xampp/php/php.ini";
- $post _data = Array (
- "Upload" = $furl
- );
Where does curlopt_url mean to submit? Corresponds to the path specified by "Action" in the form
- $url = "http://localhost/test/curl/curl_post.php";
Setting variables
- curl_setopt ($ch, Curlopt_url, $url);
- curl_setopt ($ch, Curlopt_returntransfer, 0);//Whether the execution result is returned, 0 is returned, and 1 is not returned
- curl_setopt ($ch, Curlopt_header, 0);//parameter settings, whether the header information is displayed, 1 is displayed, 0 is not displayed
Forge a webpage source address and forge a form submission from Baidu
- curl_setopt ($ch, Curlopt_referer, "http://www.baidu.com");
form data, is the normal form setting value is not 0
- curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, curlopt_timeout, 1);//Set the maximum amount of curl execution time-out
When using arrays to provide post data, the Curl component is presumably intended to be compatible with the @filename of this upload file.
- By default the Content_Type is set to Multipart/form-data. Although for most Web servers and
- No impact, but there are still a few servers that are incompatible. This article concludes that there is no need to upload files in the
- case, try to http_build_query the data submitted by the post, and then send it out, to achieve better compatibility, smaller request packets.
- curl_setopt ($ch, Curlopt_postfields, $post _data);
Execute and get results
- $output = curl_exec ($ch);
- if ($outopt = = = FALSE)
- {
- echo "
"," CUrl Error: ". Curl_error ($ch);
- }
- Releasing the curl handle
- Curl_close ($ch);
- ?>
Copy CodeFile 2:curl_post.php
- echo "
"; - Var_dump ($_files);
- ?>
Copy CodeThe results are as follows:
- Array (1) {
- ["Upload"]=>
- Array (5) {
- ["Name"]=>
- String (7) "PHP.ini"
- ["Type"]=>
- String ("Application/octet-stream")
- ["Tmp_name"]=>
- String ("F:xampptmpphpb2d1.tmp")
- ["Error"]=>
- Int (0)
- ["Size"]=>
- Int (46217)
- }
- }
Copy Code |