PHP 使用 Fsockopen 发布数据
我正在尝试使用 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 OK 日期:2010 年 3 月 2 日,星期二 22:40:46 GMT 服务器:Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.2.6 内容长度:31 连接:关闭 内容类型:text/html;字符集=UTF-8 string(0) "" 数组 ( )
我有什么问题吗?为什么变量不发布其数据?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您的代码中有很多小错误。这是一个经过测试并且有效的代码片段。
然后在 example.com/reposter.php 把这个
当运行时你应该得到类似的输出
There are many small errors in your code. Here's a snippet which is tested and works.
And then at example.com/reposter.php put this
When run you should get output something like
$data
绝不会被写入套接字。你想添加类似的内容:At no point is
$data
being written to the socket. You want to add something like:试试这个
Try this instead
试试这个:
某些字符转义符(例如
\n
)在单引号中不起作用。Try this:
Some character escapes such as
\n
do not work in single quotes.Tamlyn 不错,效果很好!
对于那些还需要随 url 一起发送 get vars 的人,
Nice one Tamlyn, works great!
For those that also need to send get vars along with the url,
在 reposter.php 中尝试这个
,因为数据不在
$_POST[]
变量中,而是在$GLOBALS['HTTP_RAW_POST_DATA']
变量中。Try this in reposter.php
Because, the data wasn't in the
$_POST[]
variables but it was in the$GLOBALS['HTTP_RAW_POST_DATA']
variable.您可以使用此技术,它将有助于调用任意数量的页面,所有页面将立即独立运行,而无需异步等待每个页面响应。
cornjobpage.php //mainpage
testpage.php
PS:如果您想以循环方式发送网址参数,请按照以下答案操作:https://stackoverflow.com/a/41225209/6295712
you can use this technique it will help to call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.
cornjobpage.php //mainpage
testpage.php
PS:if you want to send url parameters as loop then follow this answer :https://stackoverflow.com/a/41225209/6295712
在某些情况下,Curl 太重,无法使用 post_to_host():
发布到托管 php 项目位置: http://code.google.com/p/post-to-host/
Curl is too heavy in some case, to use post_to_host():
post to host php project location: http://code.google.com/p/post-to-host/
是否使用 cURL 和选项?
Is using cURL and option?
抱歉刷新,但是对于仍然遇到此类问题的人,将 HTTP/1.0 更改为 HTTP/1.1 即可。
Sorry for refresh, but for people who still have problem like this, change HTTP/1.0 to HTTP/1.1 and it will work.