我试图使用fsockopen发布数据,然后返回结果。以下是我的当前代码:
<?php
$data="stuff=hoorah\r\n";
$data=urlencode($data);
$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "POST /script.php HTTP/1.0\r\n";
$out .= "Host: www.webste.com\r\n";
$out .= 'Content-Type: application/x-www-form-urlencoded\r\n';
$out .= 'Content-Length: ' . strlen($data) . '\r\n\r\n';
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?> 它应该回显页面,它正在回显页面,但是下面是script.php的脚本
<?php
echo "<br><br>";
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];
parse_str( $raw_data, $_POST );
//test 1
var_dump($raw_data);
echo "<br><br>":
//test 2
print_r( $_POST );
?>其结果是:
HTTP/1.1 200确定日期: Tue,2010年3月2日22:40:46 GMT服务器: Apache/2.2.3 (CentOS) X驱动-By: PHP/5.2.6内容-长度: 31连接:关闭内容-类型: text/html;charset=UTF-8字符串(0)“Array”()
我有什么问题?为什么变量不发布它的数据?
发布于 2011-01-05 21:40:44
代码中有许多小错误。这是一个经过测试和工作的片段。
<?php
$fp = fsockopen('example.com', 80);
$vars = array(
'hello' => 'world'
);
$content = http_build_query($vars);
fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
header('Content-type: text/plain');
while (!feof($fp)) {
echo fgets($fp, 1024);
}然后在example.com/reposter.php上
<?php print_r($_POST);运行时,您应该得到输出,如下所示
HTTP/1.1 200 OK
Date: Wed, 05 Jan 2011 21:24:07 GMT
Server: Apache
X-Powered-By: PHP/5.2.9
Vary: Host
Content-Type: text/html
Connection: close
1f
Array
(
[hello] => world
)
0发布于 2010-03-02 23:01:31
$data从未被写入套接字。您想要添加这样的内容:
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $data);发布于 2010-03-02 23:30:55
试一试这个
$out .= 'Content-Length: ' . strlen($data) . '\r\n';
$out .= "Connection: Close\r\n\r\n";
$out .= $data;https://stackoverflow.com/questions/2367458
复制相似问题