| Erfahrener Benutzer
Registriert seit: 06.09.2008
Beiträge: 189
| Du kannst hier nicht mit den file Funktionen arbeiten, da es sein könnt, dass auf dem Zielserver allow_url_fopen auf false gesetzt ist. Du müsstest also über eine Socket-Verbindung gehen. Dazu habe ich mal eine Klasse geschrieben, die du gerne nutzen kannst.
MfG, Andy HttpConnect.php PHP-Code: <?php
/*** * Class HttpConnect * * The HttpConnect class allows easy connecting * to a server. After connecting to your server * you are able to send a post, get or a head command. * So it is possible to send or to get different information * to or from the server. Finally it is important to mention that * this class supports secure sockets layer connections (ssl). * * @package HttpConnect * @version 1.0 * @author Andreas Wilhelm <Andreas2209@web.de> * @copyright Andreas Wilhelm **/ class HttpConnect { // protected class variables private $host; private $port; private $ssl; // tip by Denis Wronka, who said, that many servers need a referrer private $referrer; private $logStats; private $log;
/** * connect() - Sets the server-variables * * @access: public * @param Str $host * @param Int $port * @param Boo $ssl * @param Boo $log * @param Str $referrer * @return NONE */ public function connect($host='localhost', $port=80, $ssl=false, $log=false, $referrer="PHP/HTTP-Class") { // set server-variables $this->host = $host; $this->port = $port; $this->ssl = $ssl; $this->referrer = $referrer; // set if the connection with the server should be logged $this->logStats = $log; }
/** * login() - Connects to the server * * @access: private * @param Str $host * @param Int $port * @return Handle */ private function login() { if ($this->ssl == true) { // use ssl-connection and save connection handle $handle = @fsockopen('ssl://'.$this->host, $this->port); // throw error in case of failure if (!$handle) { throw new Exception("SSL-connection failed.\n"); } }
else { // connection handle is saved to $handle $handle = @fsockopen($this->host, $this->port); // throw error in case of failure if (!$handle) { throw new Exception("Connection failed.\n"); } } // switch to non-blocking mode - just return data no response set_socket_blocking($handle, true); // set timeout of the server connection stream_set_timeout($handle, 0, 200000); return $handle; }
/** * auth() - Creates a connection to a secured server * * @access: private * @param Str $user * @param Str $password * @return String */ private function auth($user, $password) { if( !empty($user) ) { $auth = 'Authorization: Basic '.base64_encode($user.':'.$password)."\r\n"; }
else { $auth = ''; } return $auth; } /** * logout() - Closes the connection * * @access: private * @return NONE */ private function logout($handle) { fclose($handle); }
/** * cmd() - Sends a command to the server * * @access: private * @param Str $cmd * @return NONE */ private function cmd($handle, $cmd) { // send the request fputs($handle, $cmd); // log the request $this->log("-> $cmd"); } /** * readSock() - Reads out the response from the server * * @access: private * @return String */ private function readSock($handle) { $response = "";
while(!feof($handle)) { $response .= fread($handle, 1025); } $this->log($response); return $response; } /** * divideReply() - Spilts up the reply into the different information * * @access: private * @param Str $reply * @return Array */ private function divideReply( $reply ) { // check HTTP-Version and status-code if( !preg_match('/^HTTP\/(1\.[01]) ([1-5][0-9]{2})/', $reply, $match) ) { return false; } // split header and body $hb = strpos($reply, "\r\n")+2; $cb = strpos($reply, "\r\n\r\n")+4; // write data into an array $parsed = array( 'HTTP_VERSION' => $match[1], 'STATUS_CODE' => $match[2], 'HEADER_FIELDS' => array(), 'CONTENT' => (string) substr($reply, $cb) ); // split up header-fields $headerFields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', substr($reply, $hb, $cb-$hb-4))); foreach( $headerFields as $headerField ) { if( preg_match('/([^:]+):(.+)/m', $headerField, $match) ) { $parsed['HEADER_FIELDS'][$this->getName($match[1])] = trim($match[2]); } } // check if tranfere-encoding is chunked if( isset($parsed['HEADER_FIELDS']['Transfer-Encoding']) && $parsed['HEADER_FIELDS']['Transfer-Encoding'] == 'chunked' ) { // encode chunked data return $this->decodeChunked($parsed); } return $parsed; } /** * getName() - Returns the name of a received HTTP-header-field * * @access: private * @param Str $string * @return String */ private function getName($string) { return preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($string))); } /** * decodeChunked() - Decodes body in chunked encoding * * @access: private * @param Arr $chunked * @return Array */ private function decodeChunked($chunked) { $body = ''; $length = NULL; $size = NULL; // save chunked body to a variable $chunk = $chunked['CONTENT'];
while(true) { // get next occurrence of CRLF $pos = @strpos($chunk, "\r\n", 0);
if( !($pos === false) && ($size === NULL) ) { // get the size of following chunk from hex $size = hexdec(substr($chunk, 0, $pos)); // get the following chunk $body .= substr($chunk, $pos+2, $size); // update the content not encoded $chunk = substr($chunk, $pos+2+$size); // update content-length $length += $size; // reset chunk-size $size = NULL; } else { // leave loop break; } } // set right Content $chunked['CONTENT'] = $body; // set right Content-Length $chunked['HEADER_FIELDS']['Content-Length'] = $length; // change Transfere-Encoding $chunked['HEADER_FIELDS']['Transfer-Encoding'] = 'token'; return $chunked; } /** * head() - Returns the HTTP-header like a GET or POST reply but without the file * * @access: public * @param Str $uri * @param Mix $params * @param Mix $cookies * @param Str $user * @param Str $password * @return Array */ public function head($uri='/', $params=false, $cookies=false, $user="", $password="") { // login to the server $handle = $this->login(); // create authorization-string $auth = $this->auth($user, $password); // prepare uri for request if (empty($uri) || ($uri{0}!='/')) { $uri = "/$uri"; } // check if parameters should be used and prepare them for request if (($params!=false) && (!empty($params))) { $params = "?$params"; } else { $params = ''; } // check if cookies should be used and prepare them for request if (($cookies!=false) && (!empty($cookies))) { $cookies = "Cookie: $cookies\r\n"; } else { $cookies = ''; } // prepare host-data $host = $this->host; if ($this->port != 80) { $host .= ':'.$this->port; } // send request to the server $this->cmd($handle, "HEAD $uri"."$params HTTP/1.1\r\n"); $this->cmd($handle, "Host: $host\r\n".$cookies.$auth); $this->cmd($handle, "User-Agent: ".$this->referrer."\r\n"); $this->cmd($handle, "Connection: close\r\n\r\n"); // get server response $reply = $this->readSock($handle); // end connection to the server $this->logout($handle); // prepare headers $data = $this->divideReply($reply); return $data; } /** * get() - Returns the HTTP-header and the output of the file * * @access: public * @param Str $uri * @param Mix $params * @param Mix $cookies * @param Str $user * @param Str $password * @return Array */ public function get($uri='/', $params=false, $cookies=false, $user="", $password="") { // login to the server $handle = $this->login(); // create authorization-string $auth = $this->auth($user, $password); // prepare uri for request if (empty($uri) || ($uri{0}!='/')) { $uri = "/$uri"; } // check if parameters should be used and prepare them for request if (($params!=false) && (!empty($params))) { $params = "?$params"; } else { $params = ''; } // check if cookies should be used and prepare them for request if (($cookies!=false) && (!empty($cookies))) { $cookies = "Cookie: $cookies\r\n"; } else { $cookies = ''; } // prepare host-data $host = $this->host; if ($this->port != 80) { $host .= ':'.$this->port; } // send request to the server $this->cmd($handle, "GET $uri"."$params HTTP/1.1\r\n"); $this->cmd($handle, "Host: $host\r\n".$cookies.$auth); $this->cmd($handle, "User-Agent: ".$this->referrer."\r\n"); $this->cmd($handle, "Connection: close\r\n\r\n"); // get server response $reply = $this->readSock($handle); // end connection to the server $this->logout($handle); // prepare headers and file $data = $this->divideReply($reply); return $data; } /** * post() - Returns the HTTP-header and the output of the file * * @access: public * @param Str $uri * @param Mix $params * @param Mix $cookies * @param Mix $fileparams * @param Mix $mimes * @param Str $user * @param Str $password * @return Array */ public function post($uri='/', $params=false, $cookies=false, $fileparams=false, $mimes=false, $user='',$password='') { // login to the server $handle = $this->login(); // create authorization-string $auth = $this->auth($user, $password); // prepare uri for request if (empty($uri) || ($uri{0}!='/')) { $uri = "/$uri"; } // check if parameters should be used and prepare them for request if (($params!=false) && (!empty($params))) { $params = "?$params"; } else { $params = ''; } // check if cookies should be used and prepare them for request if (($cookies!=false) && (!empty($cookies))) { $cookies = "Cookie: $cookies\r\n"; } else { $cookies = ''; } // prepare host-data $host = $this->host; if ($this->port != 80) { $host .= ':'.$this->port; } if (($fileparams==false) || (empty($fileparams))) { if (($params!=false) && (!empty($params))) { //prepare the attribut Content-Length $length = strlen($params); // send request and parameters to the server $this->cmd($handle, "POST ".$uri." HTTP/1.1\r\n"); $this->cmd($handle, "Host: $host\r\n".$cookies.$auth); $this->cmd($handle, "User-Agent: ".$this->referrer."\r\n"); $this->cmd($handle, "Connection: close\r\n"); $this->cmd($handle, "Content-Type: application/x-www-form-urlencoded\r\n"); $this->cmd($handle, "Content-Length: ".$length."\r\n\r\n".$params); }
else { // send request without parameters $this->cmd($handle, "POST ".$uri." HTTP/1.1\r\n"); $this->cmd($handle, "Host: $host\r\n".$cookies.$auth); $this->cmd($handle, "User-Agent: ".$this->referrer."\r\n"); $this->cmd($handle, "Connection: close\r\n"); } } else { // save the name and the value of the parameters to an array $res = split('&',$params); foreach($res as $v) { list($name, $value) = split('=', $v); $param[$name] = $value; } // do the same with the file-parameters $rep = split('&',$fileparams); foreach($rep as $v) { list($name, $value) = split('=', $v); $fparam[][$name] = $value; } // save given mimetypes to an array if ( ($mimes!=false) && (!empty($mimes)) ) { $mimeparam = split(',', $mimes); } // create array for mimetypes if no exist if (!isset($mimeparam)) { $mimeparam = array(); } // fill empty mimetype-spaces with standrad-mimetype while (count($mimeparam) < count($fparam)) { $mimeparam[]='application/octet-stream'; } // the boundary seperates the different blocks holding the parameters $boundary = md5(uniqid()); $content = ''; // start counter $key = 0;
// prepare the blocks with the file-parameters foreach($fparam as $name => $fname) { $fp = fopen($fname, 'r'); $fcontent = fread($fp, filesize($fname)); $content .= '--'.$boundary."\r\n"; $content .= 'Content-Disposition: form-data; name="'.$name.'"; filename="'.$fname.'"'."\r\n"; $content .= 'Content-Type: '.$mimeparam[$key]."\r\n\r\n"; $content .= $filecontent."\r\n"; $key++; } // prepare the blocks for the parameters foreach($param as $name => $value) { $content .= '--'.$boundary."\r\n"; $content .= 'Content-Disposition: form-data; name="'.$name.'"'."\r\n\r\n"; if (!empty($value)) { $content .= $value."\r\n"; } } $content .= '--'.$boundary."--\r\n"; // get length of the content $length = strlen($content); // send standard data $this->cmd($handle, "POST ".$uri." HTTP/1.1\r\n"); $this->cmd($handle, "Host: $host\r\n".$cookies.$auth); $this->cmd($handle, "User-Agent: ".$this->referrer."\r\n"); $this->cmd($handle, "Connection: close\r\n"); // send the the parameters and file-parameters $this->cmd($handle, "Content-Type: multipart/form-data; boundary=".$boundary."\r\n"); $this->cmd($handle, "Content-Length: ".$length."\r\n\r\n"); $this->cmd($handle, $content); } // get server response $reply = $this->readSock($handle); // end connection to the server $this->logout($handle); // prepare headers and file $data = $this->divideReply($reply); return $data; } /** * log() - Saves all request and responses into $log * * @access: private * @param Str $str * @return NONE */ private function log($str) { if($this->logStats) { $this->log .= $str; } } /** * __toString() - Prints out the logged data * * @access: public * @return String */ public function __toString() { return $this->log; } } ?>
__________________ I'm so tired of slitting the throats of people calling me a violent psychopath. |