Curl Curlopt_postfields parameters in PHP using details _php Tips

Source: Internet
Author: User
Tags curl
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&para2=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.

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.