Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- |_ |_ ._ o _| |_ _ _.._ _|
- | |\/|_)| |(_| |_)(_)(_|| (_|
- /
- */
- if (!defined('CRLF')) define('CRLF', "\r\n");
- /*
- @Request
- */
- class Request {
- /*
- @strSite
- */
- private $strSite;
- /*
- @strFile
- */
- private $strFile;
- /*
- @intPort
- */
- private $intPort = 80;
- /*
- @resFile
- */
- private $resFile = null;
- /*
- @strType
- */
- private $strType = 'POST';
- /*
- @arrData
- */
- private $arrData = array();
- /*
- @boolSended
- */
- private $boolSended = false;
- /*
- @intStatusCode
- */
- private $intStatusCode = 0;
- /*
- @intContentLength
- */
- private $intContentLength = 0;
- /*
- @arrCustomHeaders
- */
- private $arrCustomHeaders = array();
- /*
- @__construct
- */
- public function __construct($strSite, $strFile, $intPort = 80) {
- // check site
- if ($strSite == gethostbyname($strSite)) {
- // failed
- return false;
- }
- // set vars
- $this->strSite = $strSite;
- $this->intPort = $intPort;
- // connect
- return $this->connect();
- }
- /*
- @connect
- */
- private function connect() {
- $this->resFile = fsockopen($this->strSite, $this->intPort);
- if (false == $this->resFile) {
- return false;
- } else {
- return true;
- }
- }
- /*
- @setData
- */
- public function setData($arrData, $strType = 'POST') {
- // set type
- $this->strType = strtoupper($strType);
- if ($this->strType == 'POST' || $this->strType == 'GET') {
- // save parameters
- if (is_array($arrData)) {
- foreach ($arrData as $strName => $strValue) {
- $this->arrData[$strName] = urlencode($strValue);
- }
- } else {
- return false;
- }
- }
- return true;
- }
- /*
- @addHeader
- */
- public function addHeader($strHeader, $strValue) {
- $this->arrCustomHeaders[$strHeader] = $strValue;
- }
- /*
- @removeHeader
- */
- public function removeHeader($strHeader) {
- if (isset($this->arrCustomHeaders[$strHeader])) {
- unset($this->arrCustomHeaders[$strHeader]);
- return true;
- } else {
- return false;
- }
- }
- /*
- @send
- */
- public function send() {
- if (false == $this->resFile) {
- return false;
- }
- // read data
- if (is_array($this->arrData)) {
- $intCounter = 0;
- $strData = '';
- foreach ($this->arrData as $strKey => $strValue) {
- $strData .= $strKey.'='.$strValue.(($intCounter + 1 != count($this->arrData)) ? '&' : '');
- $intCounter++;
- }
- }
- // set header
- $strHeader = '';
- $strHeader .= $this->strType.' /'.$this->strFile.' HTTP/1.1'.CRLF;
- $strHeader .= 'Host: '.$this->strSite.CRLF;
- $strHeader .= 'Content-Type: application/x-www-form-urlencoded'.CRLF;
- $strHeader .= 'Content-Length: '.strlen($strData).CRLF.CRLF;
- $strHeader .= $strData.CRLF;
- // add custom header
- foreach ($this->arrCustomHeaders as $strCustomHeader => $strCustomHeaderValue) {
- $strHeader .= $strCustomHeader.': '.$strCustomHeaderValue.CRLF;
- }
- if (false !== fwrite($this->resFile, $strHeader)) {
- $this->boolSended = true;
- return true;
- } else {
- return false;
- }
- }
- /*
- @saveStatusCode
- */
- private function saveStatusCode($arrMatch) {
- $this->intStatusCode = $arrMatch[2];
- }
- /*
- @saveContentLength
- */
- private function saveContentLength($arrMatch) {
- $this->intContentLength = $arrMatch[1];
- }
- /*
- @unchunkHTTP
- */
- public function unchunkHTTP($strData) {
- $intCounter = 0;
- $strOutData = '';
- while ($intCounter < strlen($strData)) {
- $intRawNum = substr($strData, $intCounter, strpos(substr($strData, $intCounter), CRLF) + 2);
- $intNum = hexdec(trim($intRawNum));
- $intCounter += strlen($intRawNum);
- $strChunk = substr($strData, $intCounter, $intNum);
- $strOutData .= $strChunk;
- $intCounter += strlen($strChunk);
- }
- return $strOutData;
- }
- /*
- @get
- */
- public function get($strKey = 'Content') {
- // check if http request was sended
- if (false == $this->resFile || false == $this->boolSended) {
- return false;
- }
- $strContent = '';
- // get content
- while (!feof($this->resFile)) {
- // save content
- $strContent .= fread($this->resFile, 1024);
- }
- $arrHeaders = array();
- // status code
- preg_replace_callback('#HTTP\/(1\.0|1\1.) ([0-9]{0,4})#', array($this, 'saveStatusCode'), $strContent);
- // add status code
- $arrHeaders['Status'] = $this->intStatusCode;
- // content Length
- preg_replace_callback('#Content\-Length\: (.*)#', array($this, 'saveContentLength'), $strContent);
- // add content length
- $arrHeaders['Length'] = $this->intContentLength;
- $arrContent = explode(CRLF.CRLF, $strContent);
- $strContent = str_replace(array(CLRF, BR), '', $arrContent[1]);
- // add content
- $arrHeaders['Content'] = $this->unchunkHTTP((($arrContent[1] == 0) ? '' : $strContent));
- return $arrHeaders[$strKey];
- }
- }
- /*
- usage:
- $objRequest = new Request('domain.com', 'file.php', 80);
- $objRequest->setData(array(
- 'key' => 'value'
- ), 'POST');
- $objRequest->send();
- $statusCode = $objRequest->get('Status');
- echo $objRequest->get('Content'); // or $objRequest->get();
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement