微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐