Home  >  Article  >  Backend Development  >  How to solve php curl post error problem

How to solve php curl post error problem

藏色散人
藏色散人Original
2021-03-26 09:11:352249browse

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.

How to solve php curl post error problem

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( &#39;Title&#39; => $title, &#39;Content&#39; => $content, &#39;ComeFrom&#39; => $comefrom );
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_URL, &#39;http://example.com/b.php&#39;);
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn