Da ich für meine Repository Klasse eine Verbindung zu einem anderen Server mittels HTTP brauche, schreibe ich gerade eine HTTP-Klasse. Ich habe dazu einige Methoden meiner FTP-Klasse übernommen. Eigentlich alles schön und gut, aber es funktioniert nicht und ich weiß nicht wieso. Momentan mache ich nur Testausgaben um zu schauen, ob die Grundstrukturen funktionieren, auf denen die gesamte Klasse aufbauen soll, aber das funktioniert nicht. Ich versuche momentan folgenden Verbindungsablauf mit der Klasse auszuführen. (Mit dem nachfolgenden Code klappt es auch)
- Code: Alles auswählen
<?php
// run server connection
$fp = fsockopen('www.avedo.net', 80, $errno, $error, 5)
OR // if connection failed
die($error."(". $errno.")");
//send request
fwrite($fp, "HEAD / HTTP/1.0\r\n");
// end request
fwrite($fp, "\r\n");
// change Content-Type
header('Content-Type: text/plain');
while(!feof($fp))
{
$reply = fread($fp, 1025);
$reply = str_replace("\r\n", '\r\n'."\n", $reply);
echo $reply;
}
// close connection
fclose($fp);
?>
Meine Klasse soll nun das gleiche machen. Jedoch klappt das nicht. Ich erhalte aber leider keine Fehlermeldung oder ähnliches. Ich sehe nur eine weiße Seite.
- Code: Alles auswählen
<?php
error_reporting(E_ALL);
/***
* HttpConnect class allows easy connecting to a foreign server.
*
* @package HttpConnect
* @version 0.1
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/
class HttpConnect
{
// public class variables
protected $entry;
protected $host;
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 if the connection with the server should be logged
$this->logStats = $log;
// save host-name to $host
$this->host = $host;
// control-connection handle is save to $control
$this->entry = @fsockopen($host, $port);
if (!$this->entry)
throw new Exception("Connection failed.\n");
// switch to non-blocking mode - just return data no response
set_socket_blocking($this->entry, true);
// set timeout of the server connection
stream_set_timeout($this->entry, 0, 200000);
}
/**
* Destructor, is called if the entity is deleted (via unser() at the end of the script
*
* @access: public
* @return NONE
*/
public function __destruct()
{
fclose($this->entry);
}
/**
* sendCmd() - Sends a command to the server
*
* @access: public
* @param Str $cmd
* @return NONE
*/
public function sendCmd($cmd)
{
// send the request
fwrite($this->entry, $cmd);
// log the request
$this->log("> $cmd");
}
/**
* readSock() - Reads out the response from the server
*
* @access: public
* @return String
*/
public function readSock()
{
$response = "";
while(!feof($this->entry))
{
$response .= fread($this->entry, 1025);
}
$this->log($response);
return $response;
}
/**
* log() - Saves all request to the server and their responses into $this->log
*
* @access: private
* @param Str $str
* @return NONE
*/
private function log($str)
{
if($this->logStats)
{
$this->log .= "$str<br>";
}
}
/**
* 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);
$http->sendCmd('HEAD / HTTP/1.0\r\n');
$http->sendCmd('\r\n');
echo $http->readSock();
echo $http->getLog();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
Kann mir jemand sagen, wo mein Fehler liegt? Es wird wie gesagt nur eine weiße Seite angezeigt. Keine Fehler rein garnichts.
MfG, Andy
PS: Das erste Script gibt nachfolgenden Code aus, den das untere Script auch ausgeben sollte.
HTTP/1.1 302 Found\r\n
Date: Mon, 21 Apr 2008 18:04:27 GMT\r\n
Server: Apache\r\n
X-Powered-By: PHP/5.2.0-8+etch10\r\n
Location: ./user/index.php\r\n
Connection: close\r\n
Content-Type: text/html\r\n
\r\n

