php post fsockopen,PHP fsockopen模拟POST/GET传送数据方法

$arr=array(

'user'=>'test',

'password'=>'123'

);

sock_get($post_url,$arr);

sock_post($post_url,$arr);

//fsocket模拟get提交

function sock_get($url,$query=array()){

$query_str = http_build_query($query);

$info = parse_url($url);

$port = isset($info['port'])? $info['port'] : 80;

$query_str = empty($info["query"])?$query_str:$info["query"].'&'.$query_str;

$fp = fsockopen($info["host"], $port, $errno, $errstr, 8);

if(!$fp){

return FALSE;

}

//$head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0\r\n";

$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;

}

fclose($fp);

return true;

}

//fsockopen模拟POST

function sock_post($url,$data=array()){

$query = http_build_query($data);

$info = parse_url($url);

$fp = fsockopen($info["host"], 80, $errno, $errstr, 8);

$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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值