<?php
class HTTP_Socket {
function HTTP_Socket( $proxy_settings = array('',3128,'','',30) ) { /*ip, port, user, pass, timeout (sec)*/
$this->proxy_addr = $proxy_settings[0];
$this->proxy_port = $proxy_settings[1];
$this->proxy_user = $proxy_settings[2];
$this->proxy_password = $proxy_settings[3];
$this->timeout = $proxy_settings[4];
$this->user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
}
function ProxyConnection(){
return !empty($this->proxy_addr)?"Proxy-Connection: close\r\n":"";
}
function ProxyCache(){
return !empty($this->proxy_addr)?"Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n":"";
}
function BasicAuthStr(){
return !empty($this->proxy_user)?"Proxy-Authorization: Basic "
.base64_encode ( $this->proxy_user.":".$this->proxy_password ). "\r\n":"";
}
function HTTPBuildRequest($RequestType, $host, $URL, $postdata,$cookies){
$URL = $URL?$URL:'/';
$cookies = !empty($cookies)?"Cookie: $cookies\r\n":"";
$header = "$RequestType $URL HTTP/1.0\r\n"
."User-Agent: $this->user_agent\r\n"
."Host: $host\r\n"
."Accept: */*\r\n"
."Content-Type: application/x-www-form-urlencoded\r\n"
.$cookies
.$this->ProxyConnection()
.$this->ProxyCache()
.$this->BasicAuthStr()
."Content-length: ".strlen($postdata)."\r\n\r\n"
.$postdata;
return $header;
}
function OpenSocket($host,$port){
$proxy_fp = @fsockopen ( $host, $port , $errno, $errstr, $this->timeout);
if (! $proxy_fp ){
trigger_error("$errstr ($errno)",E_USER_WARNING);
return false ;
}
return $proxy_fp;
}
function Request($url,$postdata='',$cookies=''){
$method = ($postdata=='')?'GET':'POST';
$result = '';
if($this->proxy_addr==''){
$arr = parse_url($url);
$host = $arr['host'];
$path = @$arr['path'];
$port = 80;
}else{
$host =$this->proxy_addr;
$port = $this->proxy_port;
$path = $url;
}
$proxy_fp = $this->OpenSocket($host, $port);
if ($proxy_fp===false) return false;
fputs ( $proxy_fp , $this->HTTPBuildRequest($method,$host,$path,$postdata,$cookies) );
while(! feof ( $proxy_fp )){
$result .= fread ( $proxy_fp , 4096 );
}
fclose ( $proxy_fp );
$result = split("\r\n\r\n",$result,2);
return $result[1];
}
function IsHTTPOk($R){
return (strlen($R) > 10) && ($R[9] == '2');
}
function IsHTTPMoved($R){
return (strlen($R) > 10) && ($R[9] == '3');
}
}
?>
<?php
error_reporting(E_ALL);
require_once('sockets.php');
$proxy = array("192.168.0.1",3128,"user","password",30); /*ip, port, user, pass, timeout (sec)*/
$socket = new HTTP_Socket($proxy); /*with proxy*/
//$socket = new HTTP_Socket(); /*without proxy*/
echo $socket->Request("http://www.google.ru/");
?>