How to solve php curl post error problem

藏色散人
Release: 2023-03-09 06:28:02
Original
2257 people have browsed it

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);
Copy after login

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 "

" 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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template