<?php
/**
* HTTP adapter for Stream class
*
* do ansyncronously HTTP requests
*
* @author Alexander Over <hide@address.com>
*/
class Stream_HTTP extends Stream
{
/**
* @var array $request holds the requests
*/
protected $request = array();
/**
* @var integer $port HTTP Port, default 80
*/
protected $port = 80;
/**
* Class constructor
*
* set protocol and default socket timeout
*/
public function __construct()
{
$this->protocol = 'tcp';
$this->timeout = 10;
$this->request = array();
}
/**
* add a GET request to the request array
*
* @param string $host
* @param string $filename
* @param array $data
*
* @return int $index
*/
public function addGetRequest($host, $filename = '', $index = null, $function = false, $headers = null)
{
$index = ((!is_null($index)) ? $index : count($this->request));
$this->request[$index]['path'] = $host.':'.$this->port;
$this->request[$index]['file'] = $filename;
if ( $function )
{
$this->request[$index]['cust'] = $function;
}
$this->request[$index]['data'] = "GET ".$filename." HTTP/1.1\r\n".
"Host: ".$host."\r\n".
"User-Agent: ".((array_key_exists('HTTP_USER_AGENT', $_SERVER)?$_SERVER['HTTP_USER_AGENT']:'Mozilla compatible') )."\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;application/json;image/png,image/*;q=0.8,*/*;q=0.5\r\n".
"Accept-Language: q=0.9,*/*;q=0.8\r\n".
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$this->request[$index]['data'].= ((is_array($headers)) ? implode("\r\n", $headers)."\r\n" : '').
"Connection: close\r\n".
"\r\n";
return $index;
}
/**
* add a POST request to the request array
*
* @param string $host
* @param string $filename
* @param array $data
*
* @return int $index
*/
public function addPostRequest($host, $filename = '', $index = null, $postdata, $function = false, $headers = null)
{
$poststring = ((is_array($postdata))?http_build_query($postdata):$postdata);
$index = ((!is_null($index)) ? $index : count($this->request));
$this->request[$index]['path'] = $host.':'.$this->port;
$this->request[$index]['file'] = $filename;
if ( $function )
{
$this->request[$index]['cust'] = $function;
}
$this->request[$index]['data'] = "POST ".$filename." HTTP/1.0\r\n".
"Host: ".$host."\r\n".
"User-Agent: ".((array_key_exists('HTTP_USER_AGENT', $_SERVER)?$_SERVER['HTTP_USER_AGENT']:'Mozilla compatible') )."\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;application/json;image/png,image/*;q=0.8,*/*;q=0.5\r\n".
"Accept-Language: q=0.9,*/*;q=0.8\r\n".
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$this->request[$index]['data'].= "Content-Type: application/x-www-form-urlencoded"."\r\n".
"Content-Length: ".strlen( $poststring )."\r\n".
((is_array($headers)) ? implode("\r\n", $headers)."\r\n" : '').
"Connection: close\r\n".
"\r\n".
$poststring;
return $index;
}
/**
* send the request
* @param int $maxParallel limit the count of parallel requests. 0 for no limitation
*
* @return array mixed
*/
public function doRequest($maxParallel = 0)
{
$return = parent::doRequest($maxParallel);
// Split response into header and data
if (is_array($return) && count($return))
{
foreach ($return as $key => $data)
{
$split = explode("\r\n\r\n", $data);
if (count($split) > 1)
{
$readheaders = explode("\r\n", $split[0]);
foreach ($readheaders as $header)
{
$hsplit = explode(':', $header);
$name = $hsplit[0];
array_shift($hsplit);
$headers[$key][$name] = trim(implode(':', $hsplit));
}
array_shift($split);
}
else
{
$headers = array();
}
$implode = implode("\r\n\r\n", $split);
if (is_array($headers[$key]) and (array_key_exists('Transfer-Encoding', $headers[$key]) and $headers[$key]['Transfer-Encoding'] == 'chunked') ||
(array_key_exists('Content-Transfer-Encoding', $headers[$key]) and $headers[$key]['Content-Transfer-Encoding'] == 'chunked'))
{
// chunked
$split = $this->unchunk($implode);
}
else
{
// plain
$split = $implode;
}
$result[$key] = $split;
}
return array('headers' => $headers,
'result' => $result);
} else
{
return array();
}
}
private function unchunk($result)
{
return preg_replace_callback(
'/(?:(?:\r\n|\n)|^)([0-9A-F]+)(?:\r\n|\n){1,2}(.*?)'.
'((?:\r\n|\n)(?:[0-9A-F]+(?:\r\n|\n))|$)/si',
create_function('$matches', 'return hexdec($matches[1]) == strlen($matches[2]) ? $matches[2] : $matches[0];'), $result
);
}
}