简体   繁体   English

PHP curl只发送一次POST数据

[英]PHP curl only sending POST data once

I have the following PHP code: 我有以下PHP代码:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            "https://10.0.0.99/api/login");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "somethingsomething");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/plain',
    'Content-Length: 18')
);

$cookie = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL,            "https://10.0.0.99/api/getcmds");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/plain',
    'Content-Length: 0')
);

$response = curl_exec($ch);
$json = json_decode($response, true);

curl_setopt($ch, CURLOPT_URL,            "https://10.0.0.99/api/cmd/" . $json["value"]);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "on");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/plain',
    'Content-Length: 2')
);

curl_exec($ch);

curl_close($ch);

The first call to curl_exec posts the string as expected and returns the response. 对curl_exec的第一次调用按预期方式发布了字符串,并返回了响应。 Second call also works as expected. 第二通电话也按预期工作。

The third call however doesn't post the string "on" to the server. 但是,第三个调用不会将字符串“on”发布到服务器上。 It correctly gets the returned data (which, without the "on" is an error), but the server never receives the posted data. 它可以正确获取返回的数据(如果不带“ on”,则表示错误),但是服务器从不接收发布的数据。

The code itself is running on PHP 5.5.30 in the command line using the -f flag. 代码本身在命令行中使用-f标志在PHP 5.5.30上运行。 Curl Verbose mode is showing that the Content-Length is beeing set correctly, however it doesn't show anything about posted data. Curl Verbose模式显示正确设置了Content-Length,但是未显示任何有关已发布数据的信息。 The rest of the post seems to be correct, it's just that no data arrives. 该帖子的其余部分似乎是正确的,只是没有数据到达。

(PS: This is not supposed to be production code, just something I'm playing around with, so please no comments about code quality/security.) (PS:这不应该是生产代码,而只是我在玩的东西,所以请不要评论代码质量/安全性。)

You can try closing the connection and then starting again for a new call/request. 您可以尝试关闭连接,然后重新开始进行新的呼叫/请求。 I personally do this thing. 我亲自做这件事。

From php.net docs on cURL: PHP: curl_setop 来自cURL上的php.net文档: PHP:curl_setop

As for CURL_POSTFIELDS : This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. 至于CURL_POSTFIELDS :这个参数可以作为urlencoded字符串传递,如'para1 = val1&para2 = val2&...',或者作为一个数组,字段名称为键,字段数据为值。

In your case, you set an empty variable $_POST['on'] . 在您的情况下,您设置一个空变量$_POST['on'] If you want it to have some particular value: 如果您希望它具有某些特殊的价值:

curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "on=value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM