Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- |_ |_ ._ o _| |_ _ _.._ _|
- | |\/|_)| |(_| |_)(_)(_|| (_|
- /
- */
- /*
- @HTTPRequest
- */
- class HTTPRequest {
- /*
- @strDomain
- */
- private $strDomain;
- /*
- @strFile
- */
- private $strFile;
- /*
- @intPort
- */
- private $intPort = 80;
- /*
- @resFile
- */
- private $resFile = null;
- /*
- @intType
- */
- private $intType = 1;
- /*
- @strHeader
- */
- private $strHeader = '';
- /*
- @boolSended
- */
- private $boolSended = false;
- /*
- @strResponseHeaders
- */
- private $strResponseHeaders = false;
- /*
- @POST
- */
- const POST = 1;
- /*
- @GET
- */
- const GET = 2;
- /*
- @HTTPSTATUSCODE
- */
- const HTTPSTATUSCODE = 3;
- /*
- @HTTPSTATUS
- */
- const HTTPSTATUS = 4;
- /*
- @CONTENTTYPE
- */
- const CONTENTTYPE = 5;
- /*
- @CONTENTLENGTH
- */
- const CONTENTLENGTH = 6;
- /*
- @CONNECTIONSTATUS
- */
- const CONNECTIONSTATUS = 7;
- /*
- @CRLF
- */
- const CRLF = "\r\n";
- /*
- @strResponse
- */
- private $strResponse = false;
- /*
- @intHTTPStatusCode
- */
- private $intHTTPStatusCode = 0;
- /*
- @strHTTPStatus
- */
- private $strHTTPStatus = '';
- /*
- @strContentType
- */
- private $strContentType = '';
- /*
- @intContentLength
- */
- private $intContentLength = 0;
- /*
- @strConnectionStatus
- */
- private $strConnectionStatus = 'unknown';
- /*
- @arrResponseHeaders
- */
- private $arrResponseHeaders = array();
- /*
- @__construct
- */
- public function __construct($strDomain, $strFile = '', $intPort = 80) {
- // check domain
- if ($strDomain == gethostbyname($strDomain)) {
- return false;
- }
- // set vars
- $this->strDomain = $strDomain;
- $this->strFile = $strFile;
- $this->intPort = (int)$intPort;
- // connect
- return $this->connect();
- }
- /*
- @connect
- */
- private function connect() {
- $this->resFile = @fsockopen($this->strDomain, $this->intPort);
- if ($this->resFile == false) {
- return false;
- }
- return true;
- }
- /*
- @setDefaultHeader
- */
- public function setDefaultHeader() {
- // set default header
- $this->strHeader = (($this->intType == 1) ? 'POST' : 'GET').' /'.$this->strFile.' HTTP/1.1'.self::CRLF;
- }
- /*
- @addCustomHeader
- */
- public function addCustomHeader($strHeader, $strValue) {
- // add header
- $this->strHeader .= $strHeader.((!empty($strHeader) && $strHeader != self::CRLF) ? ': ' : '').$strValue.self::CRLF;
- }
- /*
- @getHeaders
- */
- public function getHeaders() {
- return $this->strHeader;
- }
- /*
- @send
- */
- public function send($arrData = null, $intType = 2) {
- //
- $strData = '';
- $this->intType = $intType;
- if (is_array($arrData)) {
- switch ($this->intType) {
- // POST
- case 1:
- // set counter to 0
- $intCounter = 0;
- // prepare data
- foreach ($arrData as $strKey => $strValue) {
- $intCounter++;
- // add
- $strData .= $strKey.'='.urlencode($strValue).'&';
- }
- // remove last &
- $strData = substr($strData, 0, (strlen($strData) - 1));
- // save
- $this->strData = $strData;
- break;
- // GET
- case 2:
- // look for get parameters
- if (stristr($this->strFile, '?') === false) {
- $this->strFile .= '?';
- } else {
- $this->strFile .= '&';
- }
- // set counter to 0
- $intCounter = 0;
- // prepare data
- foreach ($arrData as $strKey => $strValue) {
- $intCounter++;
- // add
- $strData .= $strKey.'='.urlencode($strValue).'&';
- }
- // remove last &
- $strData = substr($strData, 0, (strlen($strData) - 1));
- // add to file
- $this->strFile .= $strData;
- break;
- }
- } else {
- $this->strData = '';
- }
- // prepare headers
- $this->setDefaultHeader();
- $this->addCustomHeader('Host', $this->strDomain);
- // post headers
- if ($this->intType == 1 && !empty($this->strData)) {
- // post header
- $this->addCustomHeader('Content-Type', 'application/x-www-form-urlencoded');
- }
- if ($this->intType == 1 && !empty($this->strData)) {
- // content length
- $this->addCustomHeader('Content-Length', strlen($this->strData));
- // data
- $this->addCustomHeader(self::CRLF, $this->strData);
- }
- // connection
- $this->addCustomHeader(self::CRLF.'Connection', 'close');
- // send
- if (false !== fwrite($this->resFile, $this->getHeaders())) {
- $this->boolSended = true;
- return true;
- } else {
- return false;
- }
- }
- /*
- @getStatus
- */
- public function getStatus() {
- return $this->boolSended;
- }
- /*
- @unchunkResponse
- */
- public function unchunkResponse($strResponse) {
- 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('$arrMatch', 'return (hexdec($arrMatch[1]) == strlen($arrMatch[2]) ? $arrMatch[2] : $arrMatch[0]);'), $strResponse);
- }
- /*
- @saveHTTPStatus
- */
- private function saveHTTPStatus($arrMatch) {
- $this->intHTTPStatusCode = (int)$arrMatch[2];
- $this->strHTTPStatus = $arrMatch[3];
- }
- /*
- @saveContentType
- */
- private function saveContentType($arrMatch) {
- $this->strContentType = $arrMatch[1];
- }
- /*
- @saveContentLength
- */
- private function saveContentLength($arrMatch) {
- $this->intContentLength = (int)$arrMatch[1];
- }
- /*
- @saveConnectionStatus
- */
- private function saveConnectionStatus($arrMatch) {
- $this->strConnectionStatus = $arrMatch[1];
- }
- /*
- @getResponse
- */
- public function getResponse() {
- if ($this->getStatus() == false) {
- return false;
- }
- if ($this->strResponse == false) {
- $strResponse = '';
- // get response
- while ($strLine = fgets($this->resFile, 128)) {
- $strResponse .= $strLine;
- }
- $this->strResponse = $strResponse;
- }
- // split response
- $arrSplitted = explode(self::CRLF.self::CRLF, $this->strResponse);
- unset($arrSplitted[1]);
- // replace
- $strContent = str_replace($arrSplitted, '', $this->strResponse);
- // save headers
- $this->strResponseHeaders = implode(self::CRLF.self::CRLF, $arrSplitted);
- return utf8_encode($this->unchunkResponse($strContent));
- }
- /*
- @getResponseHeaders
- */
- public function getResponseHeaders() {
- // check string
- if ($this->strResponseHeaders == false) {
- $this->getResponse();
- }
- // get status code
- preg_replace_callback('#HTTP\/(1.0|1.1) ([0-9]+) ([A-Za-z]+)#', array($this, 'saveHTTPStatus'), $this->strResponseHeaders);
- // get content type
- preg_replace_callback('#Content\-Type\: (.*)#', array($this, 'saveContentType'), $this->strResponseHeaders);
- // get content length
- preg_replace_callback('#Content\-Length\: ([0-9]+)#', array($this, 'saveContentLength'), $this->strResponseHeaders);
- // get connection status
- preg_replace_callback('#Connection\: ([a-z]+)#', array($this, 'saveConnectionStatus'), $this->strResponseHeaders);
- // set array
- $this->arrResponseHeaders = array(
- 'httpStatus' => array(
- 'code' => $this->intHTTPStatusCode,
- 'text' => $this->strHTTPStatus
- ),
- 'contentType' => $this->strContentType,
- 'contentLength' => $this->intContentLength,
- 'connection' => $this->strConnectionStatus
- );
- return $this->arrResponseHeaders;
- }
- /*
- @getResponseHeader
- */
- public function getResponseHeader($strHeader) {
- // check headers
- if ($this->arrResponseHeaders == false) {
- $this->getResponseHeaders();
- }
- switch ($strHeader) {
- // status code
- case 3:
- return $this->arrResponseHeaders['httpStatus']['code'];
- break;
- // status
- case 4:
- return $this->arrResponseHeaders['httpStatus']['text'];
- break;
- // content type
- case 5:
- return $this->arrResponseHeaders['contentType'];
- break;
- // content length
- case 6:
- return $this->arrResponseHeaders['contentLength'];
- break;
- // connection status
- case 7:
- return $this->arrResponseHeaders['connection'];
- break;
- default:
- return null;
- }
- }
- }
- /*
- usage:
- new http request:
- $HTTPRequest = new HTTPRequest('domain.com', 'file.php');
- send data:
- $HTTPRequest->send(array('key' => 'value'), HTTPRequest::POST); // HTTPRequest::POST // HTTPRequest::GET
- get data:
- $content = $HTTPRequest->getResponse();
- get header:
- $status = $HTTPRequest->getResponseHeader(HTTPRequest::HTTPSTATUSCODE); // 200 // 403 etc.
- $statustext = $HTTPRequest->getResponseHeader(HTTPRequest::HTTPSTATUS); // OK // forbidden etc.
- */
- ?>
Add Comment
Please, Sign In to add comment