码迷,mamicode.com
首页 > 其他好文 > 详细

fsockopen

时间:2018-04-02 13:38:49      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:sock   gpo   query   lis   open   www.   trim   inf   port   

客户端:

//fsocket模拟post提交
$url = "http://localhost/test2.php?site=nowamagic.net";
print_r(parse_url($url));

//echo $query;
sock_get($url,"user=gonn");
//sock_get($url, $query);

//fsocket模拟get提交
function sock_get($url, $query)
{
	$data = array(
	‘foo‘=>‘bar‘, 
	‘baz‘=>‘boom‘, 
	‘site‘=>‘www.nowamagic.net‘, 
	‘name‘=>‘nowa magic‘); 
	
	$query_str = http_build_query($data);
	
    $info = parse_url($url);
    $fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
    //$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;
    }
}

sock_post($url,"user=gonn");

function sock_post($url, $query)
{	
    $info = parse_url($url);
    $fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
    $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;
    }
}

  接收端:

$data = $_REQUEST;

echo ‘<pre>‘;
print_r( $data );
echo ‘</pre>‘;

  设置cookie:

function httpPost($url, $data,$cookieStr=‘‘)
{
	$url_array = parse_url($url);	
	$host = $url_array[‘host‘];
	$port = isset($url_array[‘port‘])?($url_array[‘port‘]):80;
	
	 if(!($conn = fsockopen($host,$port,$errno, $errstr, 30)))
	{
		 return false; 
	} 
			
	$header = "POST ".$url." HTTP/1.1\r\n";
	$header.= "Host : {$host}\r\n";
	$header.= "Content-type: application/x-www-form-urlencoded\r\n";
	$header.= "Content-Length:".strlen($data)."\r\n";
	$header.= "Connection: close\r\n";
        //这里是用来写cookie的
	if (!empty($cookieStr)) {
		$header.="Cookie: ".$cookieStr."\r\n";
	}
	//注意下面开头还加了个换行,结尾是两个换行
	$header.= "\r\n{$data}\r\n\r\n";
	
	//写数据
	fwrite($conn,$header);
	
    //这里读cookie
	$cookieStr=array();
    //下面的判断,读到空行时,说明头已经结束了,接下来是内容。
    while( ($line=trim(fgets($conn))) != "" )   
        {   
			$header.=$line; /*   */
            if(strstr($line,"Set-Cookie:"))   
            {   
                list($coo,$cookieLine)=explode(" ",$line);
                $cookieStr[] = $cookieLine;
            } 
        } 
    //if($len <= 0)   
    //{   
    //    return false; 
   // } 
    //读数据
    //$body=fread($conn,$len); 
    while (!feof($conn)) {
	  $body .= fread($conn, 8192);
	}
      
	fclose($conn);
	
	$result[‘body‘] = $body;
	$result[‘cookieArr‘] = $cookieStr;
	
	return $result;
}

  

fsockopen

标签:sock   gpo   query   lis   open   www.   trim   inf   port   

原文地址:https://www.cnblogs.com/callmelx/p/8692176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!