Показать сообщение отдельно

Укажите на ошибки(класс работы с сокетами по http)
  #1  
Старый 23.07.2007, 17:58
invlose
Banned
Регистрация: 06.05.2007
Сообщений: 87
С нами: 10008506

Репутация: 230
По умолчанию Укажите на ошибки(класс работы с сокетами по http)

Всем привет!
Написал если так можно выразиться
класс для работы по http через сокеты(для себя)
1.Просьба указать ошибки или недочеты по ООП
2.тоже самое но уже по PHP
3.Можно ругаться,нельзя флудить=\


PHP код:
<?
##################################################################################################
#@@@@@@@@@@@@@@@@@@
#Конструтор
#__construct($r-флаг ошибок,$u-урл,$m-метод,$h-ассоциативный массив заголовков,$d-данные для пост запроса)
#в заголовке поддерживаются:
#User-Agent
#Referer
#Content-type
#Cache
#Accept
#Connection
#@@@@@@@@@@@@@@@@@@
#Посылает заголовок
#SendData($proxy = 0,$port = 0)
#Можно указать прокси и порт
#@@@@@@@@@@@@@@@@@@
#GetData($ih-флаг,$ib-флаг,$file-путь к файлу куда сохранять данные)
#$ih :
#-1 ничего не делать
# 0 прочитать $this->get_headers
# 1 прочитать и записать в файл
#$ib
#-1 ничего не делать
# 0 прочитать (ih =-1 тогда вместе с заголовком) $this->get_body
# 1 прочитать и записать  (ih =-1 тогда вместе с заголовком)
#@@@@@@@@@@@@@@@@@@
# Деструктор
# __destruct()
# Без комментариев
#
# скачивать можно любые файлы (не только html  страницы)
# (c) By me
###################################################################################################
class mySocket
{

    public 
$get_headers;#заголовок который сичтывается
    
public $get_body;# тело ,те то что после заголовка
    
public $headers# заголовки которые формируются #паблик намеренно
    
private $report;# флаг ошибок-рапортов 0-не показывать
    
private $fp;# дискриптор сокета
    
private $host;# хост
    
    
private function er($string)#Печатает сообщения с датой
    
{
       if(
$this->report)
        echo 
date("d.m.Y G:i:s -> ").$string;
    }

    public function 
__construct($r,$u,$m 'GET',$h,$d 0)
    {
      
        
$this->report $r;
        
$this->er"#COSTRUCTION# \r\n" );
        
$this->headers='';
        
$this->body='';
        
$this->fp0;
        
$this->host='';
    
        
$ar parse_url($u);
        if(!isset(
$ar['host']))
          return -
1;
      
        
$this->host =  $ar['host'];
     
    
        if(
$m !== 'POST')
            
$this->headers.= 'GET '.$u." HTTP/1.0\r\n";
        else
            
$this->headers.= 'POST '.$u." HTTP/1.0\r\n";
    
    
        if(isset(
$h['User-Agent']))
          
$this->headers .= 'User-Agent: '.$h['User-Agent']."\r\n";
      
        
$this->headers .= 'Host: '.$this->host."\r\n";
      
        if(isset(
$h['Accept']))
          
$this->headers .= 'Accept: '.$h['Accept']."\r\n";
    
        if(isset(
$h['Referer']))
          
$this->headers .= 'Referer: '.$h['Referer']."\r\n";
      
        if(isset(
$h['Cookie']))
          
$this->headers .= 'Cookie: '.$h['Cookie']."\r\n";
        
        if(isset(
$h['Connection']))
          
$this->headers .= 'Connection: '.$h['Connection']."\r\n";
      
        if(isset(
$h['Cache']))
          
$this->headers .= 'Cache: '.$h['no-cache']."\r\n";  

        if(
$d !== 0)
        {
          
$this->headers.= 'Content-Length: '.strlen($d)."\r\n";
          
$this->headers.= "Content-Type: application/x-www-form-urlencoded\r\n"
        }

        
$this->headers .= "\r\n";
      
        if( 
$d !== 0)  
        {
    
          
$this->headers.=$d;
    
        }
    
        return 
1;
    
    }
#end construstor
    
    
    
public function SendData($proxy 0,$port 0)
    {
        
        if(
$proxy === 0)
        {
          
$port 80;
          
$proxy $this->host;
        }
    
      
#echo $this->headers;
    
        
$this->fp fsockopen($proxy$port,$errno$errstr90);
        if(!
$this->fp)
        {
            
$this->er"#ERROR_SOCKOPEN#\r\n" );
            return -
2;
        }
      
        
$this->er"#OPEN_CONNECTION#\r\n" );
      
        
fputs($this->fp$this->headers);
            
$this->er"#SEND_DATA#\r\n" );
        return 
1;
    }
#END-SEND
    
    
    
    
    
public function GetData($ih,$ib,$file 'file.txt')
    {
    
#flag-->read,write
    # -1 0 1 -1 1 0
    #
    #
    #
    #
    #
    #
        
$s 0;
        
$c 0;
    
        if(
$ih>|| $ib>0)
        {
          
$save fopen($file,'wb');
          if(!
$save)
          {
              
fclose($this->fp);
              
$this->fp 0;
              return -
3;
           }  
           
$this->er"#FILE_OPENED_FOR_WRITTING#\r\n" );
        }  
    
        if(
$ih > -1)
        {
            while(!
feof($this->fp))
            {

                
$c fgets($this->fp,1024);
                
$this->get_headers .=$c;
                if(
$c == "\r\n")
                {
                    break;
                }
            }
       
        
$this->er"#READ_DATA_HEADER#\r\n" );
       
        }
    
        if(
$ih 0)
        {
            
$this->er"#WRITE_DATA_HEADER#\r\n" );
            
fwrite($save,$this->get_headers);   
        }
      
        if(
$ib > -1)
        {
            while(!
feof($this->fp))
            {
                
$c fread($this->fp,1024);
                
$this->get_body .=$c;
            }
        
            
$this->er"#READ_DATA_BODY#\r\n" );
            if(
$ib 0)
            {
                
fwrite($save,$this->get_body);
                
$this->er"#WRITE_DATA_BODY#\r\n" );
            }   
        }
    
    
        if(
$ih>|| $ib>0)
        {
            
$this->er"#FILE_SAVED_AND_CLOSED#\r\n" );
            
fclose($save);
        }  
    
        
fclose($this->fp);
        
$this->fp 0;
        
$this->er"#DISCONNECTED#\r\n" );
        return 
1;
    
    }
#END_GET_REQUEST
    
    
    
public function __destruct() #destructor
    
{
       
$this->er"#DESTRUCTION# \r\n" );

       if(
$this->fp)
         
fclose($this->fp);
       unset(
$this->fp);
       unset(
$this->report);
       unset(
$this->headers);
       unset(
$this->host);
       unset(
$this->get_body);
       unset(
$this->get_headers);  
        
    }
    
    
}
#end class
###############################
###############################
###############################
?>
То же самое По этой ссылке будет всегда последняя версия после исправления ошибок


Пример
PHP код:
<?
header
("Content-type: image/png");
require(
'lib/socket.inc');#пропиши свой путь
function XXX()
{
  
$x = new mySocket(0,'http://chevy-rezzo.narod.ru/graphic-pool/u/4d/1/u4d12003.png','GET',array('User-Agent'=>'Opera/9.21'));
  
$x->SendData();
  
$x->GetData(0,0);
  
//echo $x->get_headers;
  
echo $x->get_body;
}
XXX();
?>

Последний раз редактировалось invlose; 23.07.2007 в 20:54..
 
Ответить с цитированием