在 a.php 中以 POST 方式向 b.php 提交数据,但是 b.php 下就是无法接收到数据,而 CURL 操作又显示成功,非常诡异。原来,“传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded。
",而和我一样对 CURL 不太熟悉的人在编写程序时,代码往往是下面的样子:
$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);
也就是将所要提交的数据以数组的形式通过 POST 发送,而这样就会导致 CURL 使用“错误"的编码“multipart/form-data",其效果相当于我们直接以“
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