Use details of the curlopt_postfields parameter in phpcurl

Source: Internet
Author: User
Use details of the curlopt_postfields parameter in phpcurl

When curl is used to send some data to the server without looking at the details, the following results may be obtained. this is not the ideal result:

  1. [Content_type] => multipart/form-data; boundary = ---------- f924413ea122

However, if you use http_build_query ($ post_data) to replace $ post_data and submit data to this php script, different results will be obtained. this is the ideal result:

  1. [Content_type] => application/x-www-form-urlencoded

As shown in the preceding example, when curl is used and the parameter is data, the http header will send content_type: application/x-www-form-urlencoded when you submit data to the server. This is a normal webpage

The header sent by the browser when the form is submitted. While multipart/form-data knows that this is a form used to upload files. Contains boundary delimiters, which can contain many more bytes.

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 arrays to provide post data, the curl component is probably intended to be compatible with the writing method of the uploaded file such as @ filename. by default, content_type is set to multipart/form-data. Although it does not affect most servers, a small number of servers are not compatible.

Conclusion: If you do not need to upload files, perform http_build_query on the data submitted by post as much as possible, and then send it out to achieve better compatibility and smaller request packets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.