Home > php教程 > PHP开发 > Usage details of CURLOPT_POSTFIELDS parameter of CURL in PHP

Usage details of CURLOPT_POSTFIELDS parameter of CURL in PHP

高洛峰
Release: 2016-12-23 15:18:35
Original
1295 people have browsed it

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

curl_setopt( $ch, CURLOPT_POSTFIELDS,$post_data);
Copy after login

But this writing method is not very useful sometimes, and may get a 500 error returned by the server. But when we try to use Socket to submit data to the server, we will get very correct results.
For example, we have the following PHP file on the server:

<?php print_r($_SERVER);?>
Copy after login

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:

[CONTENT_TYPE] => multipart/form-data; boundary=—————————-f924413ea122
Copy after login

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:

[CONTENT_TYPE] => application/x-www-form-urlencoded
Copy after login

From above It is not difficult to see from this 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:

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&…&#39; 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.
Copy after login

When using an array to provide post data, the CURL component is probably to be compatible with the @filename way of writing uploaded files. By default, the content_type is set to multipart/form-data. Although it has no impact on most servers, there are still a few 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.


For more details on the use of CURLOPT_POSTFIELDS parameters of CURL in PHP, please pay attention to the PHP Chinese website!


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template