http request mit php
Juli 2nd, 2006 by admin
für alle die webseiten über bzw. mit php aufrufen möchten, habe ich mal ne "http request" klasse geschieben.
folgende features werden unterstützt:
-get daten an eine webseite schicken
-post daten an eine webseite schicken ('PUT','DELETE','TRACE','CONNECT' werden wie post behandelt)
-es ist möglich den ganzen request über ein proxy server laufen zu lassen
-cookies können übergeben werden
-in post requests können dateien hochgeladen werden ('echte' und 'fake' dateien)
-beliebige header informationen können den request hinzugefügt werden
-alle relevanten daten können nach dem senden abgefragt werden
-ein beispiel aufruf der klasse ist in der datei einthalten
-
//Usage example:
-
//$request = new http_request('POST','macosbrain','/nvtracker/test.php');///method, host, file, port(optional), http_v(optional)
-
//$request->proxy('192.168.0.1',8080); //use a proxy server for request -> optional
-
//$request->add_header('Accept','text/plain');///add some header informations
-
//$request->add_header('User-Agent','Firefox/1.5.0.4');///add some header informations
-
//$request->add_header('Referer','http://localhost');///add some header informations
-
-
//$cookies = array(
-
// 'uid' => 3,
-
// 'pass' => 'testpass',
-
// 'PHPSESSID' => '123456789');
-
//$request->add_cookies($cookies); /// add the cookies to the request
-
//$request->add_data('disk','on'); // add "disk" with value "on"
-
//$request->add_data('test','123');// add "test" with value "123"
-
//$request->add_file('file_1',"test.jpg",$request->get_bin_file_content('C:\\small.JPG'));//add a real file, with fake name(like an upload)
-
//$request->add_file('file_2',"nice_dummy.txt",'content in the file');//add a custom "file" (like an upload)
-
-
//$request->send();///send the request
-
//echo('---------packet send--------');
-
//print_r($request->packet);
-
//echo('---------send_header---------');
-
//print_r($request->send_header);
-
//echo('---------send_data---------');
-
//print_r($request->send_data);
-
//echo('---------response from host---------');
-
//print_r($request->response);
-
-
class http_request
-
{
-
function __construct($type,$host,$file,$port = 80,$http_v = 'HTTP/1.1')
-
{
-
$this->host = $host;
-
$this->port = $port;
-
if(!$this->set_type($type))
-
die('sorry: please set a request type');
-
$this->file = $file;
-
$this->http_v = $http_v;
-
$this->add_header('Host',$host);
-
$this->add_header('Connection','Close');
-
}
-
-
function proxy($host,$port)
-
{
-
$this->con = @$this->connect($host,$port);
-
if(!$this->con)
-
die('sorry: the proxy on host "'.$host.'" did not response on port '.$port);
-
$this->proxy_con = true;
-
}
-
-
function connect($host,$port)
-
{
-
if(!is_numeric($port))
-
die('sorry: but this is not a port');
-
preg_match('@^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}$@i', $host, $ip);
-
if($ip[0]!='')
-
return fsockopen($host,$port);
-
else
-
return fsockopen(gethostbyname($host),$port);
-
}
-
-
function get_ascii_file_content($file)
-
{
-
$content = @file_get_contents($file);
-
if($content=='')
-
die('sorry: can not access file or file is empty');
-
return $content;
-
}
-
-
function get_bin_file_content($file)
-
{
-
$handle = @fopen($file, "rb");
-
if(!$handle)
-
die('sorry: can not access file');
-
$content = fread ($handle, filesize($file));
-
fclose ($handle);
-
return $content;
-
}
-
-
function send($set_ct = true)
-
{
-
if($set_ct==true)
-
$this->set_content_type();
-
if($this->proxy_con != true)
-
{
-
$this->con = @$this->connect($this->host,$this->port);
-
if(!$this->con)
-
die('sorry: "'.$this->host.'" did not response on port '.$this->port);
-
}
-
-
$this->send_data = '';
-
$contentlength = 0;
-
-
if($this->type=='GET')
-
{
-
$get_str='';
-
foreach($this->data as $name => $value)
-
{
-
$get_str .= $name.'='.$value.'&';
-
}
-
$this->file.='?'.substr($get_str,0,-1);
-
-
}
-
else //// all other request types (POST, CONNECT ...)
-
{
-
if($this->boundary)
-
{
-
foreach($this->data as $name => $value)
-
{
-
$contentlength += strlen("--".$this->boundary."\nContent-Disposition: form-data; name=\"".$name."\"\n\n".$value."\n");
-
$this->send_data.=("--".$this->boundary."\nContent-Disposition: form-data; name=\"".$name."\"\n\n".$value."\n");
-
}
-
$contentlength += strlen($this->boundary)+3;
-
$this->add_header('Content-length',$contentlength);
-
$this->send_data.= "--".$this->boundary."--\n";
-
}
-
else
-
{
-
foreach($this->data as $name => $value)
-
{
-
$this->send_data.= $name.'='.$value.'&';
-
}
-
$this->send_data = substr($this->send_data,0,-1);
-
$contentlength = strlen($this->send_data);
-
$this->add_header('Content-length',$contentlength);
-
}
-
}
-
if($this->proxy_con==true)
-
$this->send_header = $this->type.' '.'http://'.$this->host.$this->file.' '.$this->http_v."\r\n";
-
else
-
$this->send_header = $this->type.' '.$this->file.' '.$this->http_v."\r\n";
-
foreach($this->header as $head_name => $head_value)
-
$this->send_header .= $head_name.': '.$head_value."\r\n";
-
$this->send_header .= "\n";
-
$this->packet = $this->send_header.$this->send_data;
-
$this->send_to_host($this->packet);
-
}
-
-
function send_to_host($packet)
-
{
-
if(get_resource_type($this->con)!='stream')
-
die('sorry: no connection');
-
fputs($this->con,$packet);
-
if($this->proxy_con==true)
-
{
-
$this->response='';
-
while (!feof($this->con)) {
-
$this->response.=fgets($this->con);
-
}
-
}
-
else
-
{
-
$this->response='';
-
while ((!feof($this->con)) or (!eregi(chr(0x0d).chr(0x0a).chr(0x0d).chr(0x0a),$this->response))) {
-
$this->response.=fread($this->con,1);
-
}
-
}
-
fclose($this->con);
-
}
-
-
function set_content_type()
-
{
-
if($this->files> 0)
-
{
-
$this->boundary = '---------------------------'.rand(1,9999999999);
-
$this->add_header('Content-Type','multipart/form-data; boundary='.$this->boundary);
-
}
-
elseif($this->type!='GET')
-
$this->add_header('Content-Type','application/x-www-form-urlencoded');
-
}
-
-
function add_cookies($cookies)
-
{
-
if(!is_array($cookies))
-
die('sorry: we need an array for the cookies');
-
foreach($cookies as $cookie_name => $cookie_value)
-
$cookie_str .= $cookie_name.'='.$cookie_value.'; ';
-
$cookie_str = substr($cookie_str,0,-2);
-
$this->add_header('Cookie',$cookie_str);
-
}
-
-
function set_type($name)
-
{
-
$name = strtoupper($name);
-
$types = array('GET','HEAD','POST','PUT','DELETE','TRACE','CONNECT');
-
if( in_array($name,$types) )
-
{
-
$this->type = $name;
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
}
-
-
function add_data($name,$value)
-
{
-
$this->data[$name] = $value;
-
}
-
-
function add_file($name,$filename,$content)
-
{
-
if($this->type=='GET')
-
die('sorry: please do not use file uploads in get requests');
-
$this->files ++;
-
$this->add_data($name.'"; filename="'.$filename,$content);
-
}
-
-
function add_header($name,$value)
-
{
-
$this->header[$name]=$value;
-
}
-
}
-
?>
Posted in misc |
August 21st, 2007 at 17:55
Bei mir funktioniert das ganze nur, wenn die Methode
__construct
in
http_request
umbenannt ist
August 21st, 2007 at 18:04
das kann gut sein. dann nutzt du noch php 4.
in php 5 wurde die oop jedoch deutlich verbessert und es gibt richtige konstruktoren und destruktoren.