name = $ua; if (isset($DNSMode)) $this->UseDNScache = $DNSMode; } //This should prevent many redundant dns query function gethostIP($aHost) { static $hosts; //static to allow hosts to be shared between all instances if (count($hosts)>0) { foreach($hosts as $host => $IP) if ($host==$aHost) return $IP; } $IP = gethostbyname($aHost); if ($this->UseDNScache) $hosts[$aHost]=$IP; //check if we use a DNS cache return $IP; } function send_request($url,$headonly=false) { if (($url_array=parse_url($url)) !=FALSE) { $host = $url_array['host']; $hostip = $this->gethostIP($host); //5/12/2000 if (!isset($url_array['path'])) $url_array['path'] = '/'; $this->f=new socket($hostip,80,20,20,20); if (!($this->f->eof())) { $buf=(($headonly)?"HEAD":"GET")." ".$url_array['path']; // $this->f->write((($headonly)?"HEAD":"GET")." ".$url_array['path']); if (isset($url_array['query'])) $buf.='?'.$url_array['query']; // $this->f->write('?'.$url_array['query']); $buf.=" HTTP/1.1\r\n"; // $this->f->write(" HTTP/1.1\r\n"); $this->f->write($buf); $this->f->write("Host: ".$url_array['host']."\r\n" ); $this->f->write("User-Agent: ".$this->name."\r\n" ); //Needed to tell the server to close the connection once the transfer is over. This speed up things $this->f->write("Connection: close\r\n"); $this->f->write("Accept: */*\r\n" ); $this->f->write( "\r\n" ); return; //Everything went fine, let's just return } else { $this->RC = -1; $this->RCString = $this->f->errstring; } } else { $this->RC=-1; $this->RCString='malformed URL'; } $this->f->close(); //Let's close the socket to indicate the communication is done unset($this->f); } function data_available() { if (!isset($this->f)) return true; //If the socket couldn't be opened we shouldn't wait for any reply //return $this->f->data_available(); //echo('asking for socket state
'); return $this->f->receive_complete(); } function get_reply() { if (isset($this->f)) { //Any socket to get data from ? if (!($this->f->eof())) { $Status = $this->f->readline();} if (preg_match('@(\d{3}) (.*)@',$Status,$reg_array)) { $this->RC = $reg_array[1]; $this->RCString = $reg_array[2]; } while(!($this->f->eof())) { $line = $this->f->readline(); if ($line=="\r\n") break; list($name, $val) = explode(': ',$line); if (empty($val)) {$val = 'N/A';} $this->Header[strtolower($name)]=trim($val); } while (!($this->f->eof())) { $this->Content.=$this->f->readline();} $this->f->close(); //Let's close the socket to tell communication is over unset($this->f); } } //Return a specific header content function GetHeader($name) { if (isset($this->Header[$name])) return $this->Header[$name]; else return ''; } //Some function to allow to carry on some information about the page we are fetching function set_info($var) { if (is_array($var)) $this->info=array_merge($this->info, $var); } function get_info() { return $this->info; } //This function will decode the "chunked' transfer coding //as defined in RFC2068 19.4.6 function chunked_decode() { if ($this->getHeader('transfer-encoding')!='chunked') return; $buffer=$this->Content; $length = 0; $new=''; $chunkend = strpos($buffer,"\r\n")+2; $temp = substr($buffer,0,$chunkend); $chunk_size=hexdec(trim($temp)); $chunkstart=$chunkend; while ($chunk_size > 0) { $chunkend=strpos($buffer,"\r\n",$chunkstart+$chunk_size); if ($chunkend==FALSE) { //Just in case we got a broken connection $chunk=substr($buffer,$chunkstart); $new.=$chunk; $length+=strlen($chunk); break; } $chunk=substr($buffer,$chunkstart,$chunkend-$chunkstart); $new.=$chunk; $length += strlen($chunk); $chunkstart=$chunkend+2; $chunkend=strpos($buffer,"\r\n",$chunkstart)+2; if ($chunkend==FALSE) break; //Just in case we got a broken connection $temp=substr($buffer,$chunkstart,$chunkend-$chunkstart); $chunk_size=hexdec(trim($temp)); $chunkstart=$chunkend; } //Any additional header : not implemented yet // read entity-header // while (entity-header not empty) { // append entity-header to existing header fields // read entity-header // } //Update headers $this->Header['content-length'] = $length; unset($this->Header['transfer-encoding']); $this->Content=$new; } }; //end class ua ?>