Because of project requirements, the local server needs to receive data and then forward the data to another server. Therefore, it is necessary to use a post request to send data. of course, the data also contains file streams. Curl is one of the common php methods. the general code is as follows:
- $ Params1 = "test ";
- $ Params2 = "@". $ absolute_path; // if it is a file, the parameter is "@" + absolute path
- $ Post_data = array (
- 'Params1 '=> $ params1,
- 'Params2 '=> $ params2,
- );
- Function postData ($ url, $ data ){
- $ Ch = curl_init ();
- $ Timeout = 300;
- Curl_setopt ($ ch, CURLOPT_URL, $ url); // request address
- // Curl_setopt ($ ch, CURLOPT_REFERER, $ ip); // Construct a route
- Curl_setopt ($ ch, CURLOPT_POST, true); // post request
- Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true); // binary stream
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // data
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // when CURLOPT_RETURNTRANSFER is set to 1, $ head has the returned value of the request.
- Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); // you can specify the request timeout value.
- $ Handles = curl_exec ($ ch );
- Curl_close ($ ch );
- Return $ handles;
- }
The other party is a java Server. I only know the interface and I don't know how the other party processes the received files. The above method is successful in the Windows wamp environment, but the code fails to be put on the centOS + Nginx server, and the returned message is an error in receiving the file. After packet capture analysis, we found that the formats of the packages delivered by Windows 7 wamp are different from those of the http packages delivered by centos nginx. In general, curl sets content_type to multipart/form-data by default. this is true under win7 wamp on my machine, however, in centos nginx, it is application/x-www-form-urlencoded. Of course, this may also be a problem with server configuration, but I don't know where the problem is. Then I checked the PHP version, which is PHP5.3.X but slightly different. It is not ruled out that the PHP version is a problem. Then add the code:
- $ Header = array (
- 'Content-Type: multipart/form-data ',
- );
- Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
Set the header, but it is still invalid in centos. The content-type cannot be changed. Later, with the help of the technical director, I saw a link http://php.net/manual/en/class on the official PHP website. curlfile. php, refer to the official website practices in win wamp and centos nginx post requests are successful. After carefully reading the code, I found that the entire http request body was written, instead of the curl self-generated part, and I had to admire it. The following code is released:
- Function postData ($ url, $ data = array (), $ data1 = array ()){
- $ Header = array (
- 'Content-Type: multipart/form-data ',
- );
- $ Ch = curl_init ();
- Curl_setopt ($ ch, CURLOPT_URL, $ url );
- Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 );
- Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true );
- // Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
- Curl_custom_postfields ($ ch, $ data, $ data1 );
- $ Dxycontent = curl_exec ($ ch );
- Curl_close ($ ch );
- Return $ dxycontent;
- }
-
- /**
- * For safe multipart POST request for PHP5.3 ~ PHP 5.4.
- *
- * @ Param resource $ ch cURL resource
- * @ Param array $ assoc "name => value"
- * @ Param array $ files "name => path"
- * @ Return bool
- */
- Function curl_custom_postfields ($ ch, array $ assoc = array (), array $ files = array ()){
- // Invalid characters for "name" and "filename"
- Static $ disallow = array ("\ 0", "\" "," \ r "," \ n ");
-
- // Build normal parameters
- Foreach ($ assoc as $ k =>v v ){
- $ K = str_replace ($ disallow, "_", $ k );
- $ Body [] = implode ("\ r \ n", array (
- "Content-Disposition: form-data; name = \" {$ k }\"",
- "",
- Filter_var ($ v ),
- ));
- }
-
- // Build file parameters
- Foreach ($ files as $ k => $ v ){
- Switch (true ){
- Case false ===$ v = realpath (filter_var ($ v )):
- Case! Is_file ($ v ):
- Case! Is_readable ($ v ):
- Continue; // or return false, throw new InvalidArgumentException
- }
- $ Data = file_get_contents ($ v );
- $ V = call_user_func ("end", explode (DIRECTORY_SEPARATOR, $ v ));
- $ K = str_replace ($ disallow, "_", $ k );
- $ V = str_replace ($ disallow, "_", $ v );
- $ Body [] = implode ("\ r \ n", array (
- "Content-Disposition: form-data; name = \" {$ k} \ "; filename = \" {$ v }\"",
- "Content-Type: application/octet-stream ",
- "",
- $ Data,
- ));
- }
-
- // Generate safe boundary
- Do {
- $ Boundary = "-------------------". md5 (mt_rand (). microtime ());
- } While (preg_grep ("/{$ boundary}/", $ body ));
-
- // Add boundary for each parameters
- Array_walk ($ body, function (& $ part) use ($ boundary ){
- $ Part = "-- {$ boundary} \ r \ n {$ part }";
- });
-
- // Add final boundary
- $ Body [] = "-- {$ boundary }--";
- $ Body [] = "";
-
- // Set options
- Return @ curl_setopt_array ($ ch, array (
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => implode ("\ r \ n", $ body ),
- CURLOPT_HTTPHEADER => array (
- "Secondary CT: 100-continue ",
- "Content-Type: multipart/form-data; boundary = {$ boundary}", // change Content-Type
- ),
- ));
- }
Parameter transfer is not affected. if the file is in the absolute path, + "@". The only difference is that the file data and common data are distinguished by different arrays, and different processing is performed when simulating the http body part. Finally, the file is successfully uploaded. |