예제 #1
0
 public function query()
 {
     $args = func_get_args();
     $method = array_shift($args);
     $request = new Request($method, $args);
     $length = $request->getLength();
     $xml = $request->getXml();
     $r = "\r\n";
     $request = "POST {$this->path} HTTP/1.0{$r}";
     // Merged from WP #8145 - allow custom headers
     $this->headers['Host'] = $this->server;
     $this->headers['Content-Type'] = 'text/xml';
     $this->headers['User-Agent'] = $this->useragent;
     $this->headers['Content-Length'] = $length;
     foreach ($this->headers as $header => $value) {
         $request .= "{$header}: {$value}{$r}";
     }
     $request .= $r;
     $request .= $xml;
     // Now send the request
     if ($this->debug) {
         echo '<pre class="ixr_request">' . htmlspecialchars($request) . "\n</pre>\n\n";
     }
     if ($this->timeout) {
         try {
             $fp = fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
         } catch (\Exception $e) {
             $fp = false;
         }
     } else {
         try {
             $fp = fsockopen($this->server, $this->port, $errno, $errstr);
         } catch (\Exception $e) {
             $fp = false;
         }
     }
     if (!$fp) {
         return $this->handleError(-32300, 'transport error - could not open socket');
     }
     if (null !== $this->timeout_io) {
         stream_set_timeout($fp, $this->timeout_io);
     }
     fputs($fp, $request);
     $contents = '';
     $debugContents = '';
     $gotFirstLine = false;
     $gettingHeaders = true;
     while (!feof($fp)) {
         $line = fgets($fp, 4096);
         if (!$gotFirstLine) {
             // Check line for '200'
             if (strstr($line, '200') === false) {
                 return $this->handleError(-32300, 'transport error - HTTP status code was not 200');
             }
             $gotFirstLine = true;
         }
         if (trim($line) == '') {
             $gettingHeaders = false;
         }
         if (!$gettingHeaders) {
             // merged from WP #12559 - remove trim
             $contents .= $line;
         }
         if ($this->debug) {
             $debugContents .= $line;
         }
     }
     if ($this->debug) {
         echo '<pre class="ixr_response">' . htmlspecialchars($debugContents) . "\n</pre>\n\n";
     }
     // Now parse what we've got back
     $this->message = new Message($contents);
     if (!$this->message->parse()) {
         // XML error
         return $this->handleError(-32700, 'Parse error. Message not well formed');
     }
     // Is the message a fault?
     if ($this->message->messageType == 'fault') {
         return $this->handleError($this->message->faultCode, $this->message->faultString);
     }
     // Message must be OK
     return true;
 }
예제 #2
0
 /**
  * Set the query to send to the XML-RPC Server
  * @since 0.1.0
  */
 public function query()
 {
     $args = func_get_args();
     $method = array_shift($args);
     $request = new Request($method, $args);
     $length = $request->getLength();
     $xml = $request->getXml();
     $this->debugOutput('<pre>' . htmlspecialchars($xml) . PHP_EOL . '</pre>');
     //This is where we deviate from the normal query()
     //Rather than open a normal sock, we will actually use the cURL
     //extensions to make the calls, and handle the SSL stuff.
     $curl = curl_init('https://' . $this->server . $this->path);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     //Since 23Jun2004 (0.1.2) - Made timeout a class field
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
     if (null !== $this->timeout_io) {
         curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout_io);
     }
     if ($this->debug) {
         curl_setopt($curl, CURLOPT_VERBOSE, 1);
     }
     curl_setopt($curl, CURLOPT_HEADER, 1);
     curl_setopt($curl, CURLOPT_POST, 1);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
     if ($this->port !== 443) {
         curl_setopt($curl, CURLOPT_PORT, $this->port);
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: text/xml", "Content-length: {$length}"]);
     // Process the SSL certificates, etc. to use
     if (!($this->_certFile === false)) {
         // We have a certificate file set, so add these to the cURL handler
         curl_setopt($curl, CURLOPT_SSLCERT, $this->_certFile);
         curl_setopt($curl, CURLOPT_SSLKEY, $this->_keyFile);
         if ($this->debug) {
             $this->debugOutput('SSL Cert at : ' . $this->_certFile);
             $this->debugOutput('SSL Key at : ' . $this->_keyFile);
         }
         // See if we need to give a passphrase
         if (!($this->_passphrase === '')) {
             curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->_passphrase);
         }
         if ($this->_caFile === false) {
             // Don't verify their certificate, as we don't have a CA to verify against
             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
         } else {
             // Verify against a CA
             curl_setopt($curl, CURLOPT_CAINFO, $this->_caFile);
         }
     }
     // Call cURL to do it's stuff and return us the content
     $contents = curl_exec($curl);
     curl_close($curl);
     // Check for 200 Code in $contents
     if (!strstr($contents, '200 OK')) {
         //There was no "200 OK" returned - we failed
         return $this->handleError(-32300, 'transport error - HTTP status code was not 200');
     }
     if ($this->debug) {
         $this->debugOutput('<pre>' . htmlspecialchars($contents) . PHP_EOL . '</pre>');
     }
     // Now parse what we've got back
     // Since 20Jun2004 (0.1.1) - We need to remove the headers first
     // Why I have only just found this, I will never know...
     // So, remove everything before the first <
     $contents = substr($contents, strpos($contents, '<'));
     $this->message = new Message($contents);
     if (!$this->message->parse()) {
         // XML error
         return $this->handleError(-32700, 'parse error. not well formed');
     }
     // Is the message a fault?
     if ($this->message->messageType == 'fault') {
         return $this->handleError($this->message->faultCode, $this->message->faultString);
     }
     // Message must be OK
     return true;
 }