//-------------------------------------------------
// Do everything that the above cURL stuff does, but
// manually with the socket.
//-------------------------------------------------
class Socket_Connection extends RemoteXMLConnection
{
//-------------------------------------------------
// Open a socket to the url on the specified port.
//-------------------------------------------------
function createRequest($method = 'GET', $url)
{
//-------------------------------------------------
// Parse the url to get the connection data.
//-------------------------------------------------
$this->_parseUrl();
//-------------------------------------------------
// Connect...
//-------------------------------------------------
$this->_conn = fsockopen($this->_server, $this->_port, $error_number, $error_string, $this->_timeout);
if(!$this->_conn)
{
trigger_error("[ERROR] Could not open the socket.", E_USER_ERROR);
}
//-------------------------------------------------
// Make sure it times out.
//-------------------------------------------------
stream_set_timeout($this->_conn, $this->_timeout);
stream_set_blocking($this->_conn, FALSE);
//-------------------------------------------------
// Figure out what connection method we are using.
//-------------------------------------------------
if($method == 'PUT')
{
$this->_put = TRUE;
}
//-------------------------------------------------
// Set the headers.
//-------------------------------------------------
$this->_socketCommand("{$method} {$this->_path} HTTP/1.0");
$this->_socketCommand("Host: {$this->_server}");
$this->_socketCommand("User-Agent: ". REMOTE_XML_USERAGENT);
$this->_socketCommand("Connection: Close");
$this->_socketCommand("");
}
//-------------------------------------------------
// Close the socket.
//-------------------------------------------------
function closeRequest()
{
fclose($this->_conn);
}
//-------------------------------------------------
// Get the data that the socket returned.
//-------------------------------------------------
function getReturnData()
{
$data = "";
$first_line = TRUE;
$getting_headers = TRUE;
//-------------------------------------------------
// Loop through the data returned.
//-------------------------------------------------
while(!feof($this->_conn))
{
$line = fgets($this->_conn, 4096);
//-------------------------------------------------
// This is the first line, check for the 200 code
// (everything's a-okay)
//-------------------------------------------------
if($first_line)
{
if(strlen($line) != 0)
{
//-------------------------------------------------
// Did something go wrong?
//-------------------------------------------------
if(strstr($line, " 200 ") === FALSE)
{
trigger_error("[ERROR] An error occured. HTTP status code was not 200.");
}
$first_line = FALSE;
}
}
//-------------------------------------------------
// If we're not getting headers anymore, add this line to the data.
//-------------------------------------------------
if(!$getting_headers)
{
$data .= $line;
}
//-------------------------------------------------
// If we're still getting headers and this line is equal
// to CR\LF, tell the script to start recording data for
// the next loop.
//-------------------------------------------------
if($getting_headers && $line == "\r\n")
{
$getting_headers = FALSE;
}
}
return $data;
}
//--------------------------------------------
// Send data through the socket.
//--------------------------------------------
function setPutData($xml)
{
if($this->_put)
{
fputs($this->_conn, $xml, strlen($xml));
}
}
//--------------------------------------------
// Send a command to the socket.
//--------------------------------------------
function _socketCommand($command)
{
$command = $command ."\r\n";
fputs($this->_conn, $command, strlen($command));
}
}