fHeaderResponse = array(); $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() { } static public function SplitContentTypeWithCharset($inFull,&$outMime,&$outCharset) { preg_match( '`([^;]+)(?:;[^=]+=(.+))?`i', $inFull, $matches ); $outMime = ''; $outCharset = ''; if (isset( $matches[1])) $outMime = $matches[1]; if (isset( $matches[2])) $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) { if (strlen($total_post)>0) $total_post .= '&'; $total_post .= ($key.'='.urlencode($value)); } $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 GetResponseSize() { return filesize($this->GetResponseFilePath()); } 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; $low = strtolower($inName); if (isset($this->fHeaderResponse[$low])) 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()) { $fp = fopen($this->GetResponseFilePath(), "w"); 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) { $h_array = array(); foreach ($this->fRequestHeader as $h_name => $h_value) { $h_array[] = $h_name.': '.$h_value; } curl_setopt($ch, CURLOPT_HTTPHEADER,$h_array); } else curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:")); //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_HEADERFUNCTION, array(&$this,'cb_ReadHeader')); curl_setopt ($ch, CURLOPT_TIMEOUT, 120 ); // 20 sec max curl_setopt ($ch, CURLOPT_URL, $this->fUrl ); $start_time = microtime(true); $output = curl_exec ($ch); $this->fDuration = microtime(true)-$start_time; 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) { fclose($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) $pos = strpos($header_line,': '); if ($pos) { $orig_key = substr($header_line,0,$pos); $key = strtolower($orig_key); $value = substr($header_line,$pos+2/*,strlen($header_line)-$pos-4*/); // remove the CRLF $value = str_replace(array("\r","\n"), '', $value); if (isset($this->fHeaderResponse[$key])) $this->fHeaderResponse[$key][1] .= $value; else { $this->fHeaderResponse[$key] = array($orig_key,$value); } } else { if (isset($this->fHeaderResponse['___'])) $this->fHeaderResponse['___'][1] .= $header_line; else $this->fHeaderResponse['___'] = array('___',$header_line); } return strlen($header_line); } } ?>