Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define('ASARRAY',1);
- class HTTPRequest extends Socket {
- // Set Private Variables
- private $recvHeader,$recvContent;
- private $parsedHeader=array();
- private $sendHeader=array();
- private $PostVars=array();
- private $GetVars=array();
- private $method='GET';
- private $StatusCode;
- function setMethod( $method='GET' ) {
- $method = strtoupper($method);
- $this->method = $method;
- return $method;
- }
- function setHeader( $key,$value ) {
- if(is_array($key) AND is_array($value) AND count($key) == count($value)) {
- for($i = 0; $i < count($key); $i++) {
- $this->sendHeader[$key[$i]] = $value[$i];
- }
- return true;
- } elseif(!is_array($key) AND !is_array($value)) {
- $this->sendHeader[$key] = $value;
- return true;
- }
- return false;
- }
- function setVars( $method,$key,$elem ) {
- if(is_array($key) AND is_array($elem) AND count($key) == count($elem)) {
- for($i = 0; $i < count($key); $i++) {
- $this->setVars( $method,$key[$i], $elem[$i] );
- }
- } else {
- if(strtoupper($method) == 'GET') {
- $this->GetVars[$key] = $elem;
- } elseif(strtoupper($method) == 'POST') {
- $this->PostVars[$key] = $elem;
- }
- }
- return true;
- }
- function PassVarsThru( $method,$vars ) {
- if(strtoupper($method) == 'GET') {
- $this->GetVars = array_merge($this->GetVars,$vars);
- } elseif(strtoupper($method) == 'POST') {
- $this->PostVars = array_merge($this->PostVars,$vars);
- }
- return true;
- }
- function fetchURL( $site, $method='GET',$port=80 ) {
- $this->setMethod(empty($this->method) ? $method : $this->method);
- $parsed_url = parse_url($site);
- if($parsed_url['query']) {
- $pairs = explode("&",$parsed_url);
- foreach($pairs as $pair) {
- $split = explode('=',$pair);
- $this->addVar( 'GET', $split[0], $split[1] );
- }
- }
- if($this->connect($parsed_url['host'],$port)) {
- $this->send($this->_makeHeader($parsed_url['host'],$parsed_url['path']));
- if($this->_splitHeaderContent( $this->read() )) {
- $this->_statuscode();
- $this->_interpretHeader();
- return $this->recvContent;
- /*if(preg_match('/^30\d/',$this->getStatusCode)) {
- return $this->fetchURL( $this->parsedHeader['Location'] );
- } elseif(preg_match('/^20\d/',$this->getStatusCode)) {
- return $this->recvContent;
- }*/
- }
- }
- }
- function getHeader( $mod ) {
- return ($mod ? $this->parsedHeader : $this->recvHeader);
- }
- function getContent( ) {
- if(!empty($this->recvContent)) {
- return $this->recvContent;
- } else {
- return false;
- }
- }
- function getStatusCode( ) {
- return (empty($this->StatusCode) ? $this->_statuscode() : $this->StatusCode);
- }
- private function _makeHeader( $host,$path ) {
- $getvars = $this->_makeVars('GET');
- $buffer = (empty($this->method) ? 'GET' : $this->method).' '.$path.(!empty($getvars) ? '?'.$getvars : '').' HTTP/1.0'."\r\n";
- $buffer .= 'Host: '.$host."\r\n";
- if($this->method == 'POST') {
- $postvars = $this->_makeVars('POST');
- $this->addHeader( 'Content-Type','application/x-www-form-urlencoded');
- $this->addHeader( 'Content-length',strlen($postvars));
- }
- if(count($this->sendHeader) != 0) {
- foreach($this->sendHeader as $key=>$value) {
- $buffer .= $key.': '.$value."\r\n";
- }
- }
- if($this->method == 'POST') {
- $buffer .= "\r\n".$postvars;
- }
- #print $buffer;
- return $buffer;
- }
- private function _interpretHeader( ) {
- preg_match_all("/([A-Za-z\-]+): (.*?)\r\n/",$this->recvHeader,$match);
- for($i = 0; $i < count($match[1]); $i++) {
- $this->parsedHeader[$match[1][$i]] = $match[2][$i];
- }
- return $this->parsedHeader;
- }
- private function _splitHeaderContent( $buffer ) {
- $this->recvHeader = substr($buffer,0,strpos($buffer,"\r\n\r\n"));
- $this->recvContent = trim(substr($buffer,strpos($buffer,"\r\n\r\n")));
- return true;
- }
- private function _statuscode( ) {
- $arrHeader = explode("\r\n",$this->recvHeader);
- $this->parsedHeader['Query'] = trim($arrHeader[0]);
- preg_match_all("/HTTP\/\d\.\d (\d{3}) ([a-zA-Z\s\-]+)/",$arrHeader[0],$match);
- $this->StatusCode = $match[1][0];
- return $match[1][0];
- }
- private function _makeVars( $method ) {
- $vars = (strtoupper($method) == 'GET' ? $this->GetVars : (strtoupper($method) == 'POST' ? $this->PostVars : ''));
- if(!empty($vars)) {
- $output = '';
- foreach($vars as $key=>$elem) {
- $output .= $key.'='.$elem.'&';
- }
- return substr($output,0,-1);
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement