Home >Backend Development >PHP Tutorial >Solve the problems that occur when php curl post_PHP tutorial

Solve the problems that occur when php curl post_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:49747browse

In a.php, data is submitted to b.php in POST mode, but b.php cannot receive the data, and the CURL operation shows success, 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.
", and when people who are not familiar with CURL like me write programs, the code often looks like the following:

Copy the code The 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, and this will cause CURL Using the "wrong" encoding "multipart/form-data" has the same effect as directly using "
" You can try the form to complete the operation. At this time, "b.php" cannot receive data through $_POST.

So, the correct approach should be to change $data in the above example code from an array to urlencode() encoded

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/728093.htmlTechArticleSubmit data to b.php via POST in a.php, but b.php cannot receive it data, and the CURL operation shows success, which is very strange. It turns out that "passing an array to C...
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