Reasons for failure of submitting a form through POST in php curl and solutions

Source: Internet
Author: User
When I learned how to use CURL a few days ago, I encountered a problem: I submitted data to bphp in POST mode in aphp, but bphp cannot receive the data, and the CURL operation is successful, which is very strange. After searching by multiple parties, I was finally inspired. It turns out that when I learned to use CURL a few days ago, I encountered a problem. php uses the POST method to B. php submits data, but B. in php, data cannot be received, and the CURL operation is displayed as successful, which is very strange. After searching by multiple parties, I was finally inspired. It turns out that "when an array is passed to CURLOPT_POSTFIELDS, cURL will encode the data into multipart/form-data. when the handler passes a URL-encoded string, the data is encoded into application/x-www-form-urlencoded. (PS: T is a good php Q button? N: 304224365, verification: csl) ", while people who are not familiar with CURL, like me, often look like the following code when writing a program: [php] $ data = array ('title' => $ Title, 'content' => $ Content, 'comefrom' => $ ComeFrom); curl_setopt ($ ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false); curl_setopt ($ ch, CURLOPT_URL ,' http://example.com/b.php '); Curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); curl_exec ($ ch ); that is, the data to be submitted is sent in the form of an array through POST, which causes CURL to use the "error" encoding "multipart/form-data ", the effect is equivalent to" You can try this form to complete the operation. in this case, "B. php" cannot receive data through $ _ POST in any way. Therefore, the correct method is to change $ data in the above sample code from an array to a URL encoded string connection, similar to [php] $ data = 'title = '. urlencode ($ title ). '& Content = '. urlencode ($ content ). '& ComeFrom = '. urlencode ($ comefrom. Note: the difference between the two sending encoding formats is that "multipart/form-data" sets the form to MIME encoding for the purpose of transmitting binary files. if you want to upload files, this encoding is required. However, the common url data request uses the "application/x-www-form-urlencoded" format. Therefore, if the common data is transmitted in binary format, the other party cannot get the value. So pay special attention to the encoding of the sent request. By the way, record the difference between echo and print_r (). generally, echo is used to output a variable, such as [php] echo $ data; echo "I am a fool !!! "; Echo is mainly used to output strings or variables. it cannot be used to directly output the print_r () array. it is a function dedicated to directly output arrays, such as print_r ($ array ); the label and content of the array are printed in a certain format. The above is the reason and solution for the failure of submitting forms through POST in php curl. I hope this article will be helpful to php developers. thank you for reading this article.

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.