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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...