PHP fsockopen模拟POST/GET方法
2022/3/6 9:15:04
本文主要是介绍PHP fsockopen模拟POST/GET方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
fsockopen除了像上面实例模拟生成 HTTP 连接之外,还能实现很多功能,比如模拟post 和 get 传送数据的方法。
get :
<?php
$url = "http://localhost/test2.php?site=www.tbrer.com";
print_r(parse_url($url));// 解析 URL,返回其组成部分
/* get提交 */
sock_get($url,'user=gonn');
// fsocket模拟get提交
function sock_get($url,$query){
$data = array(
'foo' => 'bar',
'baz' => 'boom',
'site' => 'www.tbrer.com',
'name' => 'nowa magic'
);
$query_str = http_build_query($data);// http_build_query()函数的作用是使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串
$info = parse_url($url);
$fp = fsockopen($info["host"],80,$errno,$errstr,30);
$head = "GET " . $info['path'] . '?' . $query_str . " HTTP/1.0\r\n";
$head .= "Host: " . $info['host'] . "\r\n";
$head .= "\r\n";
$write = fputs($fp,$head);
while(!feof($fp)){
$line = fread($fp,4096);
echo $line;
}
}
?>
<?php
$url = "http://localhost/test2.php?site=www.tbrer.com";
print_r(parse_url($url));// 解析 URL,返回其组成部分
/* post提交 */
sock_post($url,'user=gonn');
// fsocket模拟post提交
function sock_post($url,$query){
$info = parse_url($url);
$fp = fsockopen($info["host"],80,$errno,$errstr,30);
$head = "POST " . $info['path'] . "?" . $info["query"] . " HTTP/1.0\r\n";
$head .= "Host: " . $info['host'] . "\r\n";
$head .= "Referer: http://" . $info['host'] . $info['path'] . "\r\n";
$head .= "Content-type: application/x-www-form-urlencoded\r\n";
$head .= "Content-Length: ". strlen(trim($query)) . "\r\n";
$head .= "\r\n";
$head .= trim($query);
$write = fputs($fp,$head);
while(!feof($fp)){
$line = fread($fp,4096);
echo $line;
}
}
?>
接收页面 test2.php 的代码为:
<?php
$data = $_REQUEST;
echo '<pre>';
print_r($data);
echo '</pre>';
?>
————————————————
原文链接:https://blog.csdn.net/zjsfdx/article/details/89376176
这篇关于PHP fsockopen模拟POST/GET方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。
- 2023-10-13唱衰这么多年,PHP 仍然还是你大爷!
- 2023-07-25PHP8,性能更好,语法更好,类型安全更完善