Php curl post problems solved

Source: Internet
Author: User

In a. php, data is submitted to B. php In POST mode, but data cannot be received in B. php, And the CURL operation is displayed as successful, which is very strange. 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.
", While people who are not familiar with CURL, like me, often look like the following code when writing a program:

Copy codeThe Code is as follows:
$ 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 through POST in the form of an array, which causes CURL to use the "error" encoding "multipart/form-data ", the effect is equivalent to "<form method =" post "action =" B. php "enctype =" multipart/form-data ">" to complete the operation, you can try it. php "cannot receive data through $ _ POST in any way.

Therefore, the correct method should be to change the $ data in the above sample code from an array to URL code () encoded.

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.