本地主机上的 fsockopen 演示

标签 fsockopen demo

任何人都可以帮我解决本地主机中的 fsockopen 问题吗?

我创建了 fsock.php 以将变量发布到同一文件夹中的 test121.php。 http://localhost/ftp/fsock.php

<?php
$fp = fsockopen('localhost/ftp');

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST /test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost/ftp \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);
}?>

这是test121.php中的代码 http://localhost/ftp/test121.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 );  
?>

我在 fsock.php 中收到无法解析地址错误。 谁能解释一下如何在本地主机中解决这个问题。 预先感谢您。

最佳答案

fsock.php

$fp = fsockopen("localhost", 80, $errno, $errstr, 30);

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST http://localhost/ftp/test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost \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);
}?>

您可能还想更改 test121.php 并删除 $GLOBALS (不确定为什么使用它):

<?php
echo "<br><br>";    
$raw_data = $_POST;

//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );  
?>

关于本地主机上的 fsockopen 演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426191/

相关文章:

android - 在哪里可以找到 Android "Fingerpaint"演示? (Android Studio 时代)

php - fsockopen 通常很慢吗?

php - 使用 php 从给定的 URL 下载文件,通过传递用户名和密码进行 http 身份验证

generics - 内存有什么好处,真的那么有用吗?

python - 我应该在哪里放置 Python 包的演示脚本?

带按钮的 PICkit2 闪烁 LED

php - 在PHP中使用fsockopen与gmail imap服务器通信时出现问题

php - 如何在 PHP 中为单元测试伪造资源?

php - 从 PHP 向 TCP/IP 控制台执行命令的最佳方式

ios - 具有相同代码库的两个项目