fsockopen

Posted callmelx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fsockopen相关的知识,希望对你有一定的参考价值。

客户端:

//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的主要内容,如果未能解决你的问题,请参考以下文章

使用 fsockopen 的警报握手失败 paypal IPN 集成

如何解决[警告:fsockopen():无法连接]?

使用 fgets/fread 从 fsockopen 读取数据挂起

fsockopen():无法连接无法使用PHP(连接超 时)

使用 fsockopen 时将 php 输出保存到变量中

PHP fsockopen() cookie