fsockopen的get和post实例

<?php
/*
**GET 提交
*/
/*
$f=fsockopen(");
$content='';
fwrite($f,"GET / HTTP/1.1\r\n");//设置请求方式
fwrite($f,"Host:www.baidu.com\r\n");
fwrite($f,"Connection:close\r\n\r\n");
while(($line=fread($f,8192))){
 $content.=$line;
}
fclose($f);
echo $content;
*/

//post提交,必须要注意的是:post数据必须要要进行rawurlencode转换,必须提交content-length的长度,post表单必须跟在\r\n\r\n

$data=rawurlencode("name").'='.rawurlencode('guoxu');
$f=fsockopen("127.0.0.1",80,$error,$errstr,60);
fwrite($f,"POST /ob/post.php?ac=post HTTP/1.1\r\n");
fwrite($f,"Host:127.0.0.1\r\n");
fwrite($f,"Content-Type:application/x-www-form-urlencoded\r\n");
fwrite($f,"Content-Length:".strlen($data)."\r\n");
fwrite($f,"Connection:Close\r\n");
fwrite($f,"Accept:*/*\r\n\r\n");
fwrite($f,$data."\r\n");
fwrite($f,"\r\n");
$str='';

while(($line=fread($f,1024))){
 $str.=$line; 
}
fclose($f);
$array=split("\n\r",$str,2);
echo $array[1];
?>

posted @ 2011-09-20 14:08  rorshach  阅读(107)  评论(0编辑  收藏  举报