Home >Backend Development >PHP Problem >How to solve php curl post error problem
Solution to php curl post error: First open the PHP code file with the error; then change the "$data" in the PHP code from an array to the data encoded by the "urlencode()" function.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
Solution to the problem when php curl post Method
has the following scenario:
Submit data to b.php in a.php in POST mode, but b.php cannot receive the data, and the CURL operation It shows success again, which is very strange. It turns out that "when passing an array to CURLOPT_POSTFIELDS, cURL will encode the data into multipart/form-data, but when passing a URL-encoded string, the data will be encoded into application/x-www-form-urlencoded.".
When people who are not familiar with CURL write programs, the code often:
Code example:
<?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);
Convert the data to be submitted as an array The form is sent through POST, which will cause CURL to use the "wrong" encoding "multipart/form-data", which is equivalent to directly using "199e07492c5873e876e3b8d071867ecd" to complete the operation. You can try it. At this time, "b.php" cannot receive data through $_POST anyway.
So, the correct approach should be to change the $data in the above code from an array to one encoded by urlencode().
[Recommended learning: PHP video tutorial]
The above is the detailed content of How to solve php curl post error problem. For more information, please follow other related articles on the PHP Chinese website!