Home  >  Article  >  Backend Development  >  Usage details of curlopt_postfields parameter of php curl

Usage details of curlopt_postfields parameter of php curl

WBOY
WBOYOriginal
2016-07-25 08:53:492471browse
Copy code

When using curl to send some data to the server without paying attention to details, you may get the following results, which Not the ideal result:

  1. [content_type] => multipart/form-data; boundary=————————-f924413ea122
Copy the code

But if you use http_build_query($post_data) instead When $post_data submits data to this php script, you will get different results from the above. This is the ideal result:

  1. [content_type] => application/x-www-form-urlencoded
Copy code

As can be seen from the above example, when using curl and the parameter is data, when submitting data to the server, http The header will send content_type: application/x-www-form-urlencoded. This is the header sent by the browser when submitting a form on a normal web page. And multipart/form-data knows that this is the form used to upload files. Including the boundary delimiter will add many bytes.

The php 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&…' 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 probably sets the content_type to multipart/form-data by default in order to be compatible with the @filename writing method for uploading files. Although it has no impact on most servers, there are still a few servers that are incompatible.

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.


Statement:
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