In general, when we use CURL to submit POST data, we are accustomed to writing this:
Copy Code code as follows:
curl_setopt ($ch, Curlopt_postfields, $post _data);
But this kind of writing in some cases will not be very useful, may be the server returned 500 errors. But when we try to submit the data to the server using the Socket, we get a very good result.
For example, we have one of the following PHP files on the server:
Copy Code code as follows:
<?php Print_r ($_server);? >
When we use CURL to send some data to the server without paying attention to the details, we may get the following result, which is not our ideal result:
Copy Code code as follows:
[Content_Type] => multipart/form-data; boundary= —————————-f924413ea122
But if we use Http_build_query ($post _data) to replace $post _data and then submit the data to this PHP script, we will get different results from the above, which is our ideal result:
Copy Code code as follows:
[Content_Type] => application/x-www-form-urlencoded
It is easy to see from the example above that when using CURL and parameters as data, HTTP headers send content_type:application/x-www-form-urlencoded when submitting data to the server. This is the normal Web page <form> submit the form when the browser sends the head. And Multipart/form-data We know this is a form for uploading files. Includes the boundary delimiter, which can be a lot more bytes.
That's what the Official Handbook says:
Copy Code code as follows:
The full data to post in a HTTP "post" operation. To post a file, prepend a filename with @ and use the full path. This can either is passed as a urlencoded string like ' Para1=val1¶2=val2& ... ' or as an array with the field name As key and field data as value. If value is a array, the Content-type header would be set to Multipart/form-data.
When using arrays to provide post data, the CURL component is probably designed to be compatible @filename the type of upload file, and the default content_type is set to Multipart/form-data. Although it does not affect most servers, there are a few servers that are not compatible.
After a summary of the final conclusion: in the absence of the need to upload files, as far as possible to the post submitted data http_build_query processing, and then sent out, can achieve better compatibility, smaller request packets.