Because of the needs of the project, the local server needs to receive data, and then forward the data to another server, it is necessary to use the analog POST request to send data, of course, the data also contains file streams. Curl is one of the most common ways PHP is used, and the general code is as follows:
- $params 1 = "Test";
- $params 2 = "@". $absolute _path;//if it is a file, the parameter is "@" + absolute path
- $post _data = Array (
- ' Params1 ' = $params 1,
- ' Params2 ' = $params 2,
- );
- function PostData ($url, $data) {
- $ch = Curl_init ();
- $timeout = 300;
- curl_setopt ($ch, Curlopt_url, $url); Request Address
- curl_setopt ($ch, Curlopt_referer, $IP);//Construction 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 o'clock $head has the requested return value
- curl_setopt ($ch, Curlopt_connecttimeout, $timeout); Set the request time-out period
- $handles = curl_exec ($ch);
- Curl_close ($ch);
- return $handles;
- }
Copy Code The other side is the Java server, I only know the interface, do not know how to handle the file received. The above approach was successful in the Win7 Wamp environment, but it failed to put the code on the Centos+nginx server, and the message returned was that the file received failed. After grasping the packet analysis, found in Win7 Wamp issued by the package and CentOS Nginx issued by the HTTP packet format is different. In general, curl defaults to Content_Type set to Multipart/form-data, on my machine Win7 Wamp, but the CentOS Nginx is application/ X-www-form-urlencoded. Of course this may be a server configuration problem, but I don't know where the problem is. Then I looked at the next PHP version, the same as php5.3.x, but with a slight difference. Also does not exclude is the PHP version of the problem. Then add the code:
- $header = Array (
- ' Content-type:multipart/form-data ',
- );
- curl_setopt ($ch, Curlopt_httpheader, $header);
Copy Code The header is set, but it still does not work under CentOS. Can not change the content-type, it is the pit father. Later, with the help of the technical Director, read a link on the official PHP website http://php.net/manual/en/class.curlfile.php, referring to the official website practice in the win Wamp and CentOS Nginx POST request has been successful. Read the code carefully, found that the procedure was completely written in the body of the HTTP request, rather than curl their own generated parts, have to admire. Release the code below:
- function PostData ($url, $data = Array (), $data 1 = 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, $data 1);
- $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 ("\", "\" "," \ r "," \ n ");
- Build normal parameters
- foreach ($assoc as $k = = $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 (
- "Expect:100-continue",
- "Content-type:multipart/form-data; boundary={$boundary} ",//Change Content-type
- ),
- ));
- }
Copy Code Parameter passing has no effect, if the file is before the absolute path + "@". The only difference is that the file data and the normal data are distinguished by different arrays, which are handled differently when simulating the body part of HTTP. Finally, the file is uploaded successfully. |