当前位置:网站首页>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);
边栏推荐
- How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
- Dialogue with opensea co creation Alex: we still only touch the tip of the iceberg of NFT capability | chain catcher
- 利用kubernetes資源鎖完成自己的HA應用
- Ctfhub web SQL injection - integer injection
- 字符串长度
- B1006 output integer in another format
- 110. 简易聊天室13:聊天室服务端
- MySQL详解 --- 聚合与分组
- The left toolbar of hbuilder is missing
- Interviewer: with the for loop, why do you need foreach??
猜你喜欢
![[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB](/img/dc/a30ccd9943e668aef8c874980a4975.jpg)
[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB
![[redis] hash type](/img/8b/2585908318f5fe88b455323d3f392e.png)
[redis] hash type

基于 RISC-V SoC 的可配置 FFT 系统设计(1)引言

How to become a senior digital IC Design Engineer (4-3)

CTFHub-Web-密码口令-弱口令

"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
![[redis] set type](/img/97/0a83016e89316849c01072044f2124.png)
[redis] set type

What is Mipi

高并发的理解与设计方案

The 10 most commonly used gadgets for waterfall project management can be built and used freely
随机推荐
Crawler exercise (IV) -- IP address problem
[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc
Kubernetes: container resource requirements and constraints (constraints)
[redis] hash type
大三下期末考试
如何成为一名高级数字 IC 设计工程师(6-6)数字 IC 验证篇:系统级仿真
【Redis】SortedSet类型
How to use project Gantt chart to make project report
110. 简易聊天室13:聊天室服务端
String segment combination
[redis] list type
Chrome browser close update Popup
【Redis】Key的层级结构
110. simple chat room 13: chat room server
Tuples of combined data types
How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
“内窥镜第一股”二闯IPO,去年亏损5个亿,核心产品商业化仍存疑 | IPO速递
Blog publishing test 3
干货丨微服务架构是什么?有哪些优点和不足?
字符串属性练习