The curlopt of Curl in PHP

Source: Internet
Author: User

 curl is really a good tool, not only in PHP but also in other operating systems, is a very useful. But if you have some parameters that are not good enough, you may not get the results you want.

In general, when we use CURL to submit POST data, we are accustomed to writing this:   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 the following PHP file on the server:     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) instead of $post _data to submit data to this PHP script, We will get different results from the above, which is our ideal result: The     code is as follows: [Content_Type] => application/x-www-form-urlencoded It's not hard to see from the example above, When you use CURL and the parameter is data, the HTTP header sends the content_type:application/x-www-form-urlencoded when the data is submitted 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. The Official Handbook says: 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 is passed as a urlencoded string like ' Para1=val1¶2=val2The ... ' or as ' 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.