当前位置:网站首页>fsockopen函数的应用
fsockopen函数的应用
2022-06-29 02:23:00 【hello php】
demo如下:
//fsockopen()用于打开一个socket网络连接,类似与python中以下代码:
"""
# 初始化socket
client = socket.socket()
# 链接服务端
client.connect(('127.0.0.1', 8000))
# 向服务端发送信息
client.send(b'this is client')
# 接受服务端消息,一次最多1024个字节
recive_msg = client.recv(1024)
"""
$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo '<pre>';
echo '*****************************************';
echo "$errstr ($errno)<br />\n";
echo '*****************************************';
echo '</pre>';
} else {
//构建响应给浏览器的http头信息
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: baidu.com\r\n";
$out .= "name:longqiqi\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
echo '<pre>';
echo '------------------------------------------------';
while (!feof($fp)) {
echo fgets($fp, 128);
}
echo '------------------------------------------------';
echo '</pre>';
fclose($fp);
}else结构体中的$out是构建响应给浏览器的http头信息格式, 不能缺少。
拼接上HTTP头信息后,把fsockopen获取到的网页内容相应给浏览器
demo2
$url = "http://localhost/test/test.php"; #url 地址必须 http://xxxxx
$port = 80;
$t = 30;
$data = array(
'foo' => 'bar',
'baz' => 'boom',
'site' => 'www.manongjc.com',
'name' => 'nowa magic');
/**fsockopen 抓取页面
* @parem $url 网页地址 host 主机地址
* @parem $port 网址端口 默认80
* @parem $t 脚本请求时间 默认30s
* @parem $method 请求方式 get/post
* @parem $data 如果单独传数据为 post 方式
* @return 返回请求回的数据
* */
function sock_data($url, $port = 80, $t = 30, $method = 'get', $data = null)
{
$info = parse_url($url);
$fp = fsockopen($info["host"], $port, $errno, $errstr, $t);
// 判断是否有数据
if (isset($data) && !empty($data)) {
$query = http_build_query($data); // 数组转url 字符串形式
} else {
$query = null;
}
// 如果用户的$url "http://www.manongjc.com/"; 缺少 最后的反斜杠
if (!isset($info['path']) || empty($info['path'])) {
$info['path'] = "/index.html";
}
// 判断 请求方式
if ($method == 'post') {
$head = "POST " . $info['path'] . " HTTP/1.0" . PHP_EOL;
} else {
$head = "GET " . $info['path'] . "?" . $query . " HTTP/1.0" . PHP_EOL;
}
$head .= "Host: " . $info['host'] . PHP_EOL; // 请求主机地址
$head .= "Referer: http://" . $info['host'] . $info['path'] . PHP_EOL;
if (isset($data) && !empty($data) && ($method == 'post')) {
$head .= "Content-type: application/x-www-form-urlencoded" . PHP_EOL;
$head .= "Content-Length: " . strlen(trim($query)) . PHP_EOL;
$head .= PHP_EOL;
$head .= trim($query);
} else {
$head .= PHP_EOL;
}
$write = fputs($fp, $head); //写入文件(可安全用于二进制文件)。 fputs() 函数是 fwrite() 函数的别名
while (!feof($fp)) {
$line = fread($fp, 4096);
echo $line;
}
}
// 函数调用
sock_data($url, $port, $t, 'post', $data);
边栏推荐
- 110. simple chat room 13: chat room server
- How to use project Gantt chart to make project report
- HBuilder左侧工具栏不见了
- Ctfhub web password default password
- Tiflash compiler oriented automatic vectorization acceleration
- Differences between web testing and app testing
- e.target与e.currentTarget的区别
- 为什么要在网站上安装SSL证书?
- 如何成为一名高级数字 IC 设计工程师(6-5)数字 IC 验证篇:覆盖率收集
- 学习太极创客 — MQTT 第二章(九)本章测试
猜你喜欢

Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused

【Redis】数据介绍 & 通用命令 & String类型

【Redis】SortedSet类型

SystemVerilog-结构体(一)

高并发的理解与设计方案

三角函数计算

To apply for a test engineer after years, the resume with high scores should be written like this

Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter

【Redis】Set类型

月薪没到30K的程序员必须要背的面试八股,我先啃为敬
随机推荐
Use code binding DataGridView control to display tables in program interface
Blog publishing test 2
“内窥镜第一股”二闯IPO,去年亏损5个亿,核心产品商业化仍存疑 | IPO速递
Temperature conversion II
字符串分段组合
Query any field of any table in JPA to the util method of entity class dto
How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
【Redis】初识 Redis
SystemVerilog-结构体(一)
Talk about the copyonwritearraylist of JUC
字符串方法练习
Configurable FFT system design based on risc-v SOC (1) Introduction
Koa 快速入門
e.target与e.currentTarget的区别
高并发的理解与设计方案
Set set
【Redis】Hash类型
Eliminate the hover effect when the button is disabled
东方财富股票开户是会有什么风险吗?东方财富开户安全吗
okcc呼叫中心的计费方式哪个最好