B
Bgag
Hallo!
Im Rahmen eines kleines Projekts bin ich dabei eine HTTP-Klasse zu basteln. Leider ist es mir nicht möglich eine der bestehenden PHPlibs zu verwenden, da das ganze möglichst ohne AddOns funktionieren soll, um die Anforderungen an den Server niedrig zu halten. Das mit der Klasse klappte bisher ganz gut, aber es fehlen noch einige Dinge, die aber noch hinzugefügt werden sollen.
Laufplan
v0.3 Hinzufügen von GET
v0.4 Hinzufügen von POST
v0.5 Hinzufügen weiterer Verbindungsarten (proxy / ssl)
v0.6 bis v0.9 Fehlerbehebung
v1.0 Final
Es bleibt also noch etwas zu tun. Leider ist in der Klasse ein Fehler, den ich nicht finde. Um genau zu sein liegt der Fehler irgendwo in der divideReply()-Methode. Diese soll die Antwort des Servers auf einen HTTP-Request zerlegen und in ein Array speichern.
In einem Testaufruf, übermittel ich via get() Parameter an eine Datei test.php, die entweder "Hello World" oder "Try again" (abhängig vom übermittelten Parameter) zurückgeben soll. Das funktioniert auch soweit, doch wird noch irgendwelcher Mist vor und hinter dieser Ausgabe mit zurückgegeben. Zudem wird der Content-Type als einziger Parameter nicht in das Array $data aufgenommen, obwohl er ebenfalls zurückgeliefert wird. Wo liegt also mein Fehler? Die Dateien test.php und HttpConnect.php, die auch den Testaufruf enthält habe ich unten angehängt. Bin für jede Hilfe sehr dankbar, da mir bisher in anderen Foren noch nicht geholfen werden konnte.
MfG, Andy
test.php
HttpConnect.php
Im Rahmen eines kleines Projekts bin ich dabei eine HTTP-Klasse zu basteln. Leider ist es mir nicht möglich eine der bestehenden PHPlibs zu verwenden, da das ganze möglichst ohne AddOns funktionieren soll, um die Anforderungen an den Server niedrig zu halten. Das mit der Klasse klappte bisher ganz gut, aber es fehlen noch einige Dinge, die aber noch hinzugefügt werden sollen.
Laufplan
v0.3 Hinzufügen von GET
v0.4 Hinzufügen von POST
v0.5 Hinzufügen weiterer Verbindungsarten (proxy / ssl)
v0.6 bis v0.9 Fehlerbehebung
v1.0 Final
Es bleibt also noch etwas zu tun. Leider ist in der Klasse ein Fehler, den ich nicht finde. Um genau zu sein liegt der Fehler irgendwo in der divideReply()-Methode. Diese soll die Antwort des Servers auf einen HTTP-Request zerlegen und in ein Array speichern.
In einem Testaufruf, übermittel ich via get() Parameter an eine Datei test.php, die entweder "Hello World" oder "Try again" (abhängig vom übermittelten Parameter) zurückgeben soll. Das funktioniert auch soweit, doch wird noch irgendwelcher Mist vor und hinter dieser Ausgabe mit zurückgegeben. Zudem wird der Content-Type als einziger Parameter nicht in das Array $data aufgenommen, obwohl er ebenfalls zurückgeliefert wird. Wo liegt also mein Fehler? Die Dateien test.php und HttpConnect.php, die auch den Testaufruf enthält habe ich unten angehängt. Bin für jede Hilfe sehr dankbar, da mir bisher in anderen Foren noch nicht geholfen werden konnte.
MfG, Andy
test.php
PHP:
<?php
if( $_GET['num'] == 45)
{
echo "Hello world!";
}
else
{
echo "Try again!";
}
?>
HttpConnect.php
PHP:
<?php
error_reporting(E_ALL);
/***
* HttpConnect class allows easy connecting to a foreign server.
*
* @package HttpConnect
* @version 0.2
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/
class HttpConnect
{
// protected class variables
protected $host;
protected $port;
protected $logStats;
protected $log;
/**
* Constructor - Is called when the class is instanced
*
* @access: public
* @param Str $host
* @param Int $port
* @param Bool $log
* @return NONE
*/
public function __construct($host='localhost', $port=80, $log = false)
{
// set server-variables
$this->host = $host;
$this->port = $port;
// 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()
{
// control-connection handle is saved to $handle
$handle = @fsockopen($this->host, $this->port);
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;
}
/**
* logout() - Closes the connection
*
* @access: private
* @return NONE
*/
private function logout($handle)
{
fclose($handle);
}
/**
* sendCmd() - Sends a command to the server
*
* @access: public
* @param Str $cmd
* @return NONE
*/
public function sendCmd($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 NONE
*/
private function divideReply($reply)
{
// separate header and content
list($head, $content) = split("\r\n\r\n", $reply);
// unpack header information
$header = array();
if( preg_match_all('#(.*): (.*)\r\n#U', $head, $result) )
{
$num = count($result[0]);
for($i=0; $i<$num; $i++)
{
$name = $result[1][$i];
$info = $result[2][$i];
$header[$name] = $info;
}
}
$data = array('head'=>$header, 'content'=>$content);
return $data;
}
/**
* head() - Returns the HTTP-header like a GET or POST reply but without the file
*
* @access: public
* @param Str $uri
* @param Str $params
* @param Str $cookies
* @return NONE
*/
public function head($uri='/', $params=false, $cookies=false)
{
// login to the server
$handle = $this->login();
// 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->sendCmd($handle, "HEAD $uri"."$params HTTP/1.1\r\n");
$this->sendCmd($handle, "Host: $host\r\n$cookies");
$this->sendCmd($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;
}
/**
* get() - Returns the HTTP-header and the output of the file
*
* @access: public
* @param Str $uri
* @param Str $params
* @param Str $cookies
* @return NONE
*/
public function get($uri='/', $params=false, $cookies=false)
{
// login to the server
$handle = $this->login();
// 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->sendCmd($handle, "GET $uri"."$params HTTP/1.1\r\n");
$this->sendCmd($handle, "Host: $host\r\n$cookies");
$this->sendCmd($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;
}
/**
* log() - Saves all request to the server and their responses into $log
*
* @access: private
* @param Str $str
* @return NONE
*/
private function log($str)
{
if($this->logStats)
{
$this->log .= $str;
}
}
/**
* getLog() - Prints out all requests to the server and their responses
*
* @access: public
* @return NONE
*/
public function getLog()
{
return $this->log;
}
}
try
{
// change Content-Type
header('Content-Type: text/plain');
$http = new HttpConnect("www.avedo.net", 80, true);
$data = $http->head();
$data2 = $http->get("httptest/test.php", "num=45");
echo $http->getLog();
print_r ($data);
print_r ($data2);
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>