我正在使用fsockopen连接并将数据发送到脚本。问题是,在接收端没有收到数据,就像你在下面的输出中看到的那样,只打印空数组。我在Tamlyns的回答中提供了我的解决方案:PHP Post data with Fsockopen。我做了'尝试创建后期参数的方式,输出没有差异。使用fsockopen发布数据,在接收端没有收到数据
我的主要脚本:
<?php
session_start();
$fp = fsockopen("192.168.1.107",
80,
$errno, $errstr, 10);
$params = "smtp=posteddata\r\n";
$params = urlencode($params);
$auth = base64_encode("kaand:kaand123");
if (!$fp) {
return false;
} else {
error_log("4");
$out = "POST /smic/testarea/fsockopen_print_post.php HTTP/1.1\r\n";
$out.= "Host: 192.168.1.107\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Authorization: Basic ".$auth;
$out.= 'Content-Length: '.strlen($params).'\r\n';
$out.= "Connection: Close\r\n\r\n";
$out .= $params;
fwrite($fp, $out);
fflush($fp);
header('Content-type: text/plain');
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
?>
fsockopen_print_post.php:
<?php
session_start();
print_r($_POST);
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];
parse_str($raw_data, $_POST);
print_r($_POST);
?>
输出:
HTTP/1.1 200 OK
Date: Mon, 19 Mar 2012 09:21:06 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.10
Set-Cookie: PHPSESSID=i4lcj5mn1ablqgekb1g24ckbg5; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 20
Connection: close
Content-Type: text/html; charset=UTF-8
Array
(
)
Array
(
)
有什么问题和我如何解决它?
感谢您的回答。这真的有必要吗?检查fsockopen的例子,他们不会像这样([php.net](http://se2.php.net/manual/en/function.fsockopen.php))和所有其他我找到的例子,这不会在任何地方使用。我只想发布数据,然后断开连接(应该采用异步方式)。 – Nicsoft 2012-03-19 10:19:51
如果您希望能够在不阻塞的情况下处理多个客户端,这是必要的。如果你“知道”,你只需要一个,这通常意味着你现在做,但将来需要更多,但有一个更简单的方法。但我建议你以这种方式实施解决方案,以免你未来头痛。此外,当你弄清楚这是如何工作的时候,你会失去一切,并获得更好的知识。 ;) – deed02392 2012-03-19 10:24:04
反正现在的学习曲线非常陡峭;)也许这有助于处理多个客户端,但是这有助于我在接收端获取发布的数据,这是我所要求的吗? – Nicsoft 2012-03-19 10:26:44