Beispiel #1
0
function xu_rpc_http_concise_curl($arrayParam)
{
    // 1) set up variables and verify.
    extract($arrayParam);
    if (!isset($boolSecure)) {
        $boolSecure = false;
    }
    if (!isset($boolDebug)) {
        $boolDebug = false;
    }
    if (!isset($objOutput)) {
        $objOutput = array(version => "xmlrpc");
    }
    if (isset($txtMethod) and isset($txtURL) and isset($txtHost) and isset($intPort) and isset($txtURI)) {
        // 2) xmlrpc encode request.
        $txtRequest = xmlrpc_encode_request($txtMethod, $arrayArgs, $objOutput);
        $intContentLen = strlen($txtRequest);
        dbg1("opening curl to {$txtURL}", $boolDebug);
        $txtTransport = 'HTTPS';
        if ($boolSecure) {
            $txtTransport = 'HTTPS';
        }
        $txtHTTP_Request = "POST {$txtURI} {$txtTransport}/1.0\r\n" . "User-Agent: xmlrpc-epi-php/0.2 (PHP) \r\n" . "Content-Type: text/xml\r\n" . "Content-Length: {$intContentLen}\r\n" . "\r\n" . "{$txtRequest}";
        dbg1("sending http request:</h3> <xmp>\n{$txtHTTP_Request}\n</xmp>", $boolDebug);
        // 3) open CURL, and send data.
        $objCURL = curl_init($txtURL);
        curl_setopt($objCURL, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($objCURL, CURLOPT_CUSTOMREQUEST, $txtHTTP_Request);
        curl_setopt($objCURL, CURLOPT_HEADER, 0);
        if ($a_boolSecure) {
            curl_setop($objCURL, CURLOPT_SSLVERSION, 3);
        }
        //  4) read response, and close CURL.
        $txtResponse = curl_exec($objCURL);
        curl_close($objCURL);
        dbg1("got response:</h3>. <xmp>\n{$txtResponse}\n</xmp>\n", $boolDebug);
        //  5) xmlrpc decode result.
        $objReturn = find_and_decode_xml($txtResponse, $boolDebug);
    }
    //  6) return result.
    return $objReturn;
}
 /**
  * Execute a HTTP request
  * 
  * Executes the http fetch using all the set properties. Intellegently
  * switch to fsockopen if cURL is not present. And be smart to follow
  * redirects (if asked so).
  * 
  * @param string URL of the target page (optional)
  * @param string URL of the referrer page (optional)
  * @param string The http method (GET or POST) (optional)
  * @param array Parameter array for GET or POST (optional)
  * @return string Response body of the target page
  */
 public function execute($target = '', $referrer = '', $method = '', $data = array())
 {
     // Populate the properties
     $this->target = $target ? $target : $this->target;
     $this->method = $method ? $method : $this->method;
     $this->referrer = $referrer ? $referrer : $this->referrer;
     // Add the new params
     if (is_array($data) && count($data) > 0) {
         $this->params = array_merge($this->params, $data);
     }
     // Process data, if presented
     $queryString = '';
     if ($this->rawPostData) {
         $queryString = $this->rawPostData;
     } else {
         if (is_array($this->params) && count($this->params) > 0) {
             $queryString = http_build_query($this->params);
         }
     }
     // If cURL is not installed, we'll force fscokopen
     $this->useCurl = $this->useCurl && $this->_isCurlSuitable();
     // GET method configuration
     if ($this->method == 'GET') {
         if (isset($queryString)) {
             $this->target = $this->target . "?" . $queryString;
         }
     }
     // Parse target URL
     $urlParsed = parse_url($this->target);
     if ($this->port == 0 && isset($urlParsed['port']) && $urlParsed['port'] > 0) {
         $this->port = $urlParsed['port'];
     }
     // Handle SSL connection request
     if ($urlParsed['scheme'] == 'https') {
         $this->host = $urlParsed['host'];
         $this->port = $this->port != 0 ? $this->port : 443;
         $this->_socket = 'ssl://' . $urlParsed['host'] . ':' . $this->port;
     } else {
         $this->host = $urlParsed['host'];
         $this->port = $this->port != 0 ? $this->port : 80;
         $this->_socket = 'tcp://' . $urlParsed['host'] . ':' . $this->port;
     }
     // Finalize the target path
     $this->path = (isset($urlParsed['path']) ? $urlParsed['path'] : '/') . (isset($urlParsed['query']) ? '?' . $urlParsed['query'] : '');
     $this->schema = $urlParsed['scheme'];
     // Pass the requred cookies
     $this->_passCookies();
     // Process cookies, if requested
     $cookieString = '';
     if (is_array($this->cookies) && count($this->cookies) > 0) {
         // Get a blank slate
         $tempString = array();
         // Convert cookiesa array into a query string (ie animal=dog&sport=baseball)
         foreach ($this->cookies as $key => $value) {
             if (strlen(trim($value)) > 0) {
                 $tempString[] = $key . "=" . urlencode($value);
             }
         }
         $cookieString = join('&', $tempString);
     }
     // Do we need to use cURL
     if ($this->useCurl) {
         // Initialize PHP cURL handle
         $ch = curl_init();
         // GET method configuration
         if ($this->method == 'GET') {
             curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
             curl_setopt($ch, CURLOPT_POST, FALSE);
         } else {
             curl_setopt($ch, CURLOPT_POST, TRUE);
             curl_setopt($ch, CURLOPT_HTTPGET, FALSE);
             if (isset($queryString)) {
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
             }
         }
         // Basic Authentication configuration
         if ($this->username && $this->password) {
             curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
         }
         if ($this->proxy) {
             curl_setop($ch, CURL_PROXY, $this->proxy);
         }
         // Custom cookie configuration
         if ($this->useCookie) {
             // we are sending cookies.
             if (isset($cookieString)) {
                 curl_setopt($ch, CURLOPT_COOKIE, $cookieString);
             } else {
                 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiePath);
             }
         }
         if ($this->saveCookie) {
             curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiePath);
             // Save cookies here.
         }
         curl_setopt($ch, CURLOPT_HEADER, TRUE);
         // No need of headers
         if (is_array($this->headerArray)) {
             curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArray);
         } else {
             curl_setopt($ch, CURLOPT_HEADER, TRUE);
             // No need of headers
         }
         curl_setopt($ch, CURLOPT_NOBODY, FALSE);
         // Return body
         curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
         // Timeout
         curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
         // Webbot name
         curl_setopt($ch, CURLOPT_URL, $this->target);
         // Target site
         curl_setopt($ch, CURLOPT_REFERER, $this->referrer);
         // Referer value
         curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
         // Minimize logs
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
         // No certificate
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->redirect);
         // Follow redirects
         curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirect);
         // Limit redirections to four
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
         // Return in string
         // Get the target contents
         $content = curl_exec($ch);
         if (!empty($content)) {
             $tmp = explode("\r\n\r\n", $content, 2);
             for ($i = 0; $i < count($tmp); $i++) {
                 if (empty($tmp[$i])) {
                     unset($tmp[$i]);
                 }
             }
             if (count($tmp) > 1) {
                 // Store the contents
                 $this->result = $tmp[1];
             }
             // Parse the headers
             $this->_parseHeaders($tmp[0]);
         }
         // Get the request info
         $status = curl_getinfo($ch);
         // Store the error (is any)
         $this->_setError(curl_error($ch));
         // Close PHP cURL handle
         curl_close($ch);
     } else {
         // Get a file pointer
         $filePointer = @stream_socket_client($this->_socket, $errorNumber, $errorString, $this->timeout);
         // We have an error if pointer is not there
         if (!$filePointer) {
             $this->_setError('Failed opening http socket connection: ' . $errorString . ' (' . $errorNumber . ')');
             return FALSE;
         }
         // Set http headers with host, user-agent and content type
         $this->addRequestHeader($this->method . ' ' . $this->path . "  HTTP/1.1", true);
         $this->addRequestHeader("Host: " . $this->host);
         $this->addRequestHeader('Accept: */*');
         $this->addRequestHeader("User-Agent: " . $this->userAgent);
         if (!$this->requestHeaderExists('Content-Type')) {
             $this->addRequestHeader("Content-Type: application/x-www-form-urlencoded");
         }
         // Specify the custom cookies
         if ($this->useCookie && $cookieString != '') {
             $this->addRequestHeader("Cookie: " . $cookieString);
         }
         // POST method configuration
         if ($this->method == "POST") {
             $this->addRequestHeader("Content-Length: " . strlen($queryString));
         }
         // Specify the referrer
         $this->addRequestHeader("Referer: " . $this->referrer);
         if ($this->referrer != '') {
             $this->addRequestHeader("Referer: " . $this->referrer);
         }
         // Specify http authentication (basic)
         if ($this->username && $this->password) {
             $this->addRequestheader("Authorization: Basic " . base64_encode($this->username . ':' . $this->password));
         }
         $this->addRequestHeader("Connection: close");
         // POST method configuration
         $requestHeader = implode("\r\n", $this->headerArray) . "\r\n\r\n";
         if ($this->method == "POST") {
             $requestHeader .= $queryString;
         }
         // We're ready to launch
         fwrite($filePointer, $requestHeader);
         // Clean the slate
         $responseHeader = '';
         $responseContent = '';
         // 3...2...1...Launch !
         $n = 0;
         do {
             $responseHeader .= fread($filePointer, 1);
         } while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader) && !feof($filePointer));
         // Parse the headers
         $this->_parseHeaders($responseHeader);
         // Do we have a 301/302 redirect ?
         if (($this->status == '301' || $this->status == '302') && $this->redirect == TRUE) {
             if ($this->curRedirect < $this->maxRedirect) {
                 // Let's find out the new redirect URL
                 $newUrlParsed = parse_url($this->headers['location']);
                 if ($newUrlParsed['host']) {
                     $newTarget = $this->headers['location'];
                 } else {
                     $newTarget = $this->schema . '://' . $this->host . '/' . $this->headers['location'];
                 }
                 // Reset some of the properties
                 $this->port = 0;
                 $this->status = 0;
                 $this->params = array();
                 $this->method = 'POST';
                 $this->referrer = $this->target;
                 // Increase the redirect counter
                 $this->curRedirect++;
                 // Let's go, go, go !
                 $this->result = $this->execute($newTarget);
             } else {
                 $this->_setError('Too many redirects.');
                 return FALSE;
             }
         } else {
             // Nope...so lets get the rest of the contents (non-chunked)
             if (!isset($this->headers['transfer-encoding']) || $this->headers['transfer-encoding'] != 'chunked') {
                 while (!feof($filePointer)) {
                     $responseContent .= fgets($filePointer, 128);
                 }
             } else {
                 // Get the contents (chunked)
                 while (!feof($filePointer) && ($chunkLength = hexdec(fgets($filePointer)))) {
                     $responseContentChunk = '';
                     $readLength = 0;
                     while ($readLength < $chunkLength) {
                         $responseContentChunk .= fread($filePointer, $chunkLength - $readLength);
                         $readLength = strlen($responseContentChunk);
                     }
                     $responseContent .= $responseContentChunk;
                     fgets($filePointer);
                 }
             }
             // Store the target contents
             $this->result = chop($responseContent);
         }
     }
     // There it is! We have it!! Return to base !!!
     return $this->result;
 }