Usage details of CURLOPT_POSTFIELDS parameter of CURL in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:35:42
Original
887 people have browsed it

Under normal circumstances, when we use CURL to submit POST data, we are accustomed to writing like this:

Copy code The code is as follows:
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

But this way of writing is not very useful sometimes, and you may get a 500 error returned by the server. But when we try to submit data to the server using Socket, we will get very correct results.
For example, we have the following PHP file on the server:
Copy the code The code is as follows:

When we use CURL to send some data to the server without paying attention to details, we may get the following results, which is not our ideal result:
Copy code The code is as follows:
[CONTENT_TYPE] => multipart/form-data; boundary=——————————-f924413ea122

But if we use http_build_query($post_data) instead of $post_data and then submit data to this PHP script, we will get different results from the above. This is our ideal result:
Copy code The code is as follows:
[CONTENT_TYPE] => application/x-www-form-urlencoded

From It is not difficult to see from the above example that when using CURL and the parameter is data, when submitting data to the server, the HTTP header will send Content_type: application/x-www-form-urlencoded. This is the header sent by the browser when submitting the form on a normal web page
. And multipart/form-data we know this is the form used to upload files. Including the boundary delimiter will add many bytes.
The official manual says this:
Copy the code The code is 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 be 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 an array, the Content-Type header will be set to multipart/form-data.

When using an array to provide post data, the CURL component is probably to be compatible with uploaded files like @filename The way of writing, content_type is set to multipart/form-data by default. Although it has no impact on most servers, there are still a small number of servers that are incompatible.
After some summary, we finally came to the conclusion: when there is no need to upload files, try to perform http_build_query processing on the data submitted by post, and then send it out, which can achieve better compatibility and smaller request data packets. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/742451.htmlTechArticleUnder normal circumstances, when we use CURL to submit POST data, we are accustomed to writing like this: Copy The code is as follows: curl_setopt( $ch, CURLOPT_POSTFIELDS,$post...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!