$data = array(
"data1" => "1000",
);
$data_string = json_encode($data);
$ch = curl_init("http://www.example.com/api/info/getPlan");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string))
);
$result = curl_exec($ch);
https://i.stack.imgur.com/B9fKn.png
我试图用以下代码从门户发出POST请求。并在浏览器中得到上述错误。我使用PHP 7.4,Apache 2.4.6版本。
我看到一些答案说使用CURLOPT_FOLLOWLOCATION或cookie,我尝试了一下,但我不知道该怎么做。非常感谢您的帮助。
是的,使用CURLOPT_FOLLOWLOCATION是正确的答案遵循这个示例,看看它是否适用于您
$cookie_file_path = "/toyourfile";
$data = array(
"data1" => "1000",
);
$data_string = json_encode($data);
$ch = curl_init("http://www.example.com/api/info/getPlan");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
//curl_setopt($ch, CURLOPT_VERBOSE, true); // if you run on commandline this will print out more information for you
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string))
);
$result = curl_exec($ch);