Advertising
- HttpEngine.php
- Wednesday, September 28th, 2011 at 1:01:11am MDT
- <?php
- class HttpEngine
- {
- function __construct()
- {
- $this->fUserAgent = false;
- $this->fUrl = false;
- $this->fResponse = false;
- $this->fResponseFilePath = false;
- $this->fErrorString = false;
- $this->fErrorCode = false;
- $this->fHttpCode = false;
- $this->fDuration = false;
- $this->fPostData = false;
- $this->fUsername = false;
- $this->fPassword = false;
- $this->fRequestHeader = false;
- $this->fRequestContentType = false;
- }
- function __destruct()
- {
- }
- {
- $outMime = '';
- $outCharset = '';
- $outMime = $matches[1];
- $outCharset = $matches[2];
- }
- // parameter of the request:
- function SetUserAgent($inString) { $this->fUserAgent = $inString; }
- function GetUserAgent() { return $this->fUserAgent; }
- function SetRequestContentType($inString) { $this->fRequestContentType = $inString; }
- function SetPostDataWithArray($inAssocArray)
- {
- $total_post = '';
- foreach ($inAssocArray as $key => $value)
- {
- $total_post .= '&';
- }
- $this->SetPostData($total_post);
- }
- function SetRequestHeader($inArray) { $this->fRequestHeader = $inArray; }
- function SetUsernamePassword($inUsername,$inPassword)
- {
- $this->fUsername = $inUsername;
- $this->fPassword = $inPassword;
- }
- function SetPostData($inData)
- {
- $this->fPostData = $inData;
- }
- function GetHttpCode() { return $this->fHttpCode; }
- function GetDuration() { return $this->fDuration; }
- function SetResponseFilePath($inResponseFilePath) { $this->fResponseFilePath = $inResponseFilePath; }
- function GetResponseFilePath() { return $this->fResponseFilePath; }
- function SetUrl($inString) { $this->fUrl = $inString; }
- function GetUrl() { return $this->fUrl; }
- // parameter of the response
- const kHeaderServer = 'Server';
- const kHeaderLocation = 'Location';
- const kHeaderContentType = 'Content-Type';
- function GetHeaderResponse($inName=false)
- {
- if ($inName === false)
- return $this->fHeaderResponse;
- return $this->fHeaderResponse[$low][1];
- return false;
- }
- function GetResponse() { return $this->fResponse; }
- function GetErrorString() { return $this->fErrorString; }
- function GetErrorCode() { return $this->fErrorCode; }
- function MakeRequest()
- {
- $ch = curl_init();
- $ok = false;
- if ($ch)
- {
- curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
- // CURLOPT_COOKIESESSION
- // curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout );
- // curl_setopt($ch, CURLOPT_VERBOSE, 1 );
- if ($this->fUserAgent)
- curl_setopt($ch,CURLOPT_USERAGENT,$this->fUserAgent);
- $fp = false;
- if ($this->GetResponseFilePath())
- {
- if ($fp)
- curl_setopt($ch, CURLOPT_FILE, $fp);
- }
- else
- {
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true );
- }
- if ($this->fPostData !== false)
- {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS,$this->fPostData);
- }
- //Some servers (like Lighttpd) will not process the curl request without this header and will return error code 417 instead.
- curl_setopt($ch,CURLOPT_ENCODING,"gzip,deflate");
- if ($this->fRequestContentType)
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('SOAPAction: ','Content-type: '.$this->fRequestContentType,"Expect:"));
- else if ($this->fRequestHeader)
- {
- foreach ($this->fRequestHeader as $h_name => $h_value)
- {
- $h_array[] = $h_name.': '.$h_value;
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER,$h_array);
- }
- else
- //Response will be read in chunks of 64000 bytes
- curl_setopt($ch, CURLOPT_BUFFERSIZE, 64000);
- if ($this->fUsername)
- {
- curl_setopt($ch,CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch,CURLOPT_USERPWD, $this->fUsername.':'.$this->fPassword);
- }
- //do not include headers in the response
- curl_setopt($ch, CURLOPT_HEADER, 0);
- //register a callback function which will process the headers
- //this assumes your code is into a class method, and uses $this->readHeader as the callback //function
- curl_setopt ($ch, CURLOPT_TIMEOUT, 120 ); // 20 sec max
- curl_setopt ($ch, CURLOPT_URL, $this->fUrl );
- $output = curl_exec ($ch);
- if (curl_errno($ch))
- {
- $this->fErrorCode = curl_errno($ch);
- $this->fErrorString = curl_error($ch);
- }
- if ($output !== false)
- {
- if ($this->GetResponseFilePath() === false) // response in memory
- $this->fResponse = $output;
- $this->fHttpCode = curl_getinfo ($ch, CURLINFO_HTTP_CODE );
- if ( 200 == (int) $this->fHttpCode )
- {
- $ok = true;
- $content_type = curl_getinfo ($ch, CURLINFO_CONTENT_TYPE );
- }
- }
- if ($fp)
- {
- }
- curl_close($ch);
- }
- return $ok;
- }
- private function cb_ReadHeader($ch, $header_line)
- {
- // reset to take only the last response request (in case of a redirect)
- if ($pos)
- {
- $this->fHeaderResponse[$key][1] .= $value;
- else
- {
- }
- }
- else
- {
- $this->fHeaderResponse['___'][1] .= $header_line;
- else
- }
- }
- }
- ?>
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.