fsockopen学习笔记

post请求



请求页面

<?php


$domain='www.my.com';

$url='/feedback.php?goi='.urlencode('1睡45');

$data = 'foo='.urlencode('fd踹24').'&bar='.urlencode('p2垢22');



$fp=fsockopen($domain,80);

if (!$fp)
{
    die('Unable to open');
}

$cmd="

POST $url HTTP/1.0
Content-type: application/x-www-form-urlencoded
Content-length: ".strlen($data)."
Accept: *\/*

$data

";

fputs($fp,$cmd);

$s='';

while($line=fgets($fp))
{
    $s.=$line;
}

fclose($fp);

echo '<xmp>'.$s.'</xmp>';

接收页面

<?php
echo '$_POST ';
print_r($_POST);
echo '$_GET ';
print_r($_GET);

网上借鉴的

<?php
#ini_set('display_error', 0);
error_reporting(0);
//GOGOCMS所用的类,只有看不到的,没有采集不到的!
//防止要获取的URL地址301到了其他地方
//可以启用GZIP,加快下载速度
//支持COOKIE,POST
//效率绝对超过目前网络上流传的封装好的类或函数

class Get_url{ 
    public $url = '';   //需要获取的URL 
    public $cookie = ''; //需要发送的cookies 
    public $post = false;   //模拟POST时发送的数据,默认不发送 
    public $timeout = 30;  //超时时间 
    public $return_str = '';  //获取到的数据 
    public $gzip = true;  //是否启用GZIP加快下载速度
    function __construct(){ 
        
    } 
    public function Get(){ 
        if ($this->url == '') return false; 
        $matches = parse_url($this->url);      
        !isset($matches['host']) && $matches['host'] = '';      
        !isset($matches['path']) && $matches['path'] = '';      
        !isset($matches['query']) && $matches['query'] = '';      
        !isset($matches['port']) && $matches['port'] = '';      
        $host = $matches['host']; 
        $boardurl = 'http://'.$host.'/'; 
        $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';      
        $port = !empty($matches['port']) ? $matches['port'] : 80;   
        $conf_arr = array(      
                        'limit'=>0,      
                        'post'=>$this->post,      
                        'cookie'=>$this->cookie,      
                        'bysocket'=>FALSE,      
                        'ip'=>'',      
                        'timeout'=>$this->timeout,      
                        'block'=>TRUE,      
        );  
        foreach ($conf_arr as $k=>$v) ${$k} = $v; 
        $gzip = $this->gzip ? 'gzip' : ''; 
        if ($post){ 
            if(is_array($post)) $post = http_build_query($post);//该函数需要PHP5.0以上版本才支持,如为较低版本需手工处理一下  
            $out = "POST $path HTTP/1.0\r\n";      
            $out .= "Accept: */*\r\n";      
            $out .= "Referer: $boardurl\r\n";   //伪造来路为需要获取的URL的主机名  
            $out .= "Accept-Language: zh-cn\r\n";      
            $out .= "Content-Type: application/x-www-form-urlencoded\r\n";      
            $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";      //伪造User-Agent 
            $out .= "Host: $host\r\n";      
            $out .= 'Content-Length: '.strlen($post)."\r\n";      
            $out .= "Connection: Close\r\n"; 
            $out .= "Accept-Encoding: $gzip\r\n";      //设置可以接受GZIP压缩格式的文档 
            $out .= "Cache-Control: no-cache\r\n";      
            $out .= "Cookie: $cookie\r\n\r\n";      
            $out .= $post;      
        } else {      
            $out = "GET $path HTTP/1.0\r\n";      
            $out .= "Accept: */*\r\n";      
            $out .= "Referer: $boardurl\r\n";      
            $out .= "Accept-Language: zh-cn\r\n";      
            $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";      
            $out .= "Host: $host\r\n";      
            $out .= "Connection: Close\r\n";   
            $out .= "Accept-Encoding: $gzip\r\n";  //设置可以接受GZIP压缩格式的文档 
            $out .= "Cookie: $cookie\r\n\r\n";      
        } 
        $fp = fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 
        if(!$fp) { 
            return false;      
        }else {      
        
            stream_set_blocking($fp, $block);      
            stream_set_timeout($fp, $timeout);      
            fwrite($fp, $out);    
            $status = stream_get_meta_data($fp);
            if(!$status['timed_out']) { 
                $h=''; 
                while (!feof($fp)) {      
                    if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {      
                       break;      
                    }  
                    $h .= $header; 
                } 
                $check_http = $this->check_http($h); 
                if ($check_http!==200) return $check_http; 
                $gzip = false; 
                if (strstr($h,'gzip')) $gzip = true; 
                $stop = false;      
                while( !( $stop  && $status['timed_out'] && feof($fp) ) ) { 
                    if($status['timed_out']) return false; 
                    $data = fread($fp, ($limit == 0 || $limit > 128 ? 128 : $limit));   
                    #$predata = '';
                    #$return = '';
                    if ($predata.$data==''){   //这得加个判断,有些服务器很变态……判断不了FOEF 
                        break; 
                    } 
                    $predata = $data; 
                    $return .= $data;      
                    if($limit) {      
                        $limit -= strlen($data);      
                        $stop = $limit <= 0;      
                    }      
                }      
            }      
            @fclose($fp); 
            $this->return_str = $gzip?$this->gzdecode($return):$return; 
            return true; 
        }      
    } 
    public function Check_http($h){ 
        if (strstr($h,' 301 ') || strstr($h,' 302 ')){ 
            preg_match("/Location:(.*?)$/im",$h,$match); 
            $url = trim($match[1]); 
            preg_match("/Set-Cookie:(.*?)$/im",$h,$match); 
            $cookie = trim($match[1]); 
            $r = new Get_url(); 
            $r->url = $url; 
            $r->cookie = $cookie; 
            $r_return = $r->Get(); 
            $this->return_str = $r->return_str; 
            return $r_return; 
        } 
        if (!strstr($h,' 200 ')) return false;  //如果还没有正常,那么…… 
        else return 200; 
    } 
    public function gzdecode($data){   //解压GZIP,这个函数从网上找的 
        $flags = ord(substr($data, 3, 1)); 
        $headerlen = 10; 
        $extralen = 0; 
        $filenamelen = 0; 
        if ($flags & 4) { 
            $extralen = unpack('v' ,substr($data, 10, 2)); 
            $extralen = $extralen[1]; 
            $headerlen += 2 + $extralen; 
        } 
        if ($flags & 8) $headerlen = strpos($data, chr(0), $headerlen) + 1; 
        if ($flags & 16) $headerlen = strpos($data, chr(0), $headerlen) + 1; 
        if ($flags & 2) $headerlen += 2; 
        $unpacked = @gzinflate(substr($data, $headerlen)); 
        if ($unpacked === FALSE) $unpacked = $data; 
        return $unpacked; 
    } 
} 
//以下为测试 
$get = new Get_url(); 
$get->url = "http://112.124.24.77/Public/authLogin"; 
$get->post = array('username'=>'username','password'=>'username');  //如果需要POST数据 
$get->timeout = 10; 
$get->Get(); 

#echo "<textarea>$get->return_str</textarea>"; 
echo $get->return_str;


你可能感兴趣的:(fsockopen学习笔记)