[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/IXR/ -> class-IXR-client.php (source)

   1  <?php
   2  
   3  /**
   4   * IXR_Client
   5   *
   6   * @package IXR
   7   * @since 1.5.0
   8   *
   9   */
  10  class IXR_Client
  11  {
  12      var $server;
  13      var $port;
  14      var $path;
  15      var $useragent;
  16      var $response;
  17      var $message = false;
  18      var $debug = false;
  19      var $timeout;
  20      var $headers = array();
  21  
  22      // Storage place for an error message
  23      var $error = false;
  24  
  25      /**
  26       * PHP5 constructor.
  27       */
  28      function __construct( $server, $path = false, $port = 80, $timeout = 15 )
  29      {
  30          if (!$path) {
  31              // Assume we have been given a URL instead
  32              $bits = parse_url($server);
  33              $this->server = $bits['host'];
  34              $this->port = isset($bits['port']) ? $bits['port'] : 80;
  35              $this->path = isset($bits['path']) ? $bits['path'] : '/';
  36  
  37              // Make absolutely sure we have a path
  38              if (!$this->path) {
  39                  $this->path = '/';
  40              }
  41  
  42              if ( ! empty( $bits['query'] ) ) {
  43                  $this->path .= '?' . $bits['query'];
  44              }
  45          } else {
  46              $this->server = $server;
  47              $this->path = $path;
  48              $this->port = $port;
  49          }
  50          $this->useragent = 'The Incutio XML-RPC PHP Library';
  51          $this->timeout = $timeout;
  52      }
  53  
  54      /**
  55       * PHP4 constructor.
  56       */
  57  	public function IXR_Client( $server, $path = false, $port = 80, $timeout = 15 ) {
  58          self::__construct( $server, $path, $port, $timeout );
  59      }
  60  
  61      /**
  62       * @since 1.5.0
  63       * @since 5.5.0 Formalized the existing `...$args` parameter by adding it
  64       *              to the function signature.
  65       *
  66       * @return bool
  67       */
  68      function query( ...$args )
  69      {
  70          $method = array_shift($args);
  71          $request = new IXR_Request($method, $args);
  72          $length = $request->getLength();
  73          $xml = $request->getXml();
  74          $r = "\r\n";
  75          $request  = "POST {$this->path} HTTP/1.0$r";
  76  
  77          // Merged from WP #8145 - allow custom headers
  78          $this->headers['Host']          = $this->server;
  79          $this->headers['Content-Type']  = 'text/xml';
  80          $this->headers['User-Agent']    = $this->useragent;
  81          $this->headers['Content-Length']= $length;
  82  
  83          foreach( $this->headers as $header => $value ) {
  84              $request .= "{$header}: {$value}{$r}";
  85          }
  86          $request .= $r;
  87  
  88          $request .= $xml;
  89  
  90          // Now send the request
  91          if ($this->debug) {
  92              echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n";
  93          }
  94  
  95          if ($this->timeout) {
  96              $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
  97          } else {
  98              $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
  99          }
 100          if (!$fp) {
 101              $this->error = new IXR_Error(-32300, 'transport error - could not open socket');
 102              return false;
 103          }
 104          fputs($fp, $request);
 105          $contents = '';
 106          $debugContents = '';
 107          $gotFirstLine = false;
 108          $gettingHeaders = true;
 109          while (!feof($fp)) {
 110              $line = fgets($fp, 4096);
 111              if (!$gotFirstLine) {
 112                  // Check line for '200'
 113                  if (strstr($line, '200') === false) {
 114                      $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
 115                      return false;
 116                  }
 117                  $gotFirstLine = true;
 118              }
 119              if (trim($line) == '') {
 120                  $gettingHeaders = false;
 121              }
 122              if (!$gettingHeaders) {
 123                  // merged from WP #12559 - remove trim
 124                  $contents .= $line;
 125              }
 126              if ($this->debug) {
 127                  $debugContents .= $line;
 128              }
 129          }
 130          if ($this->debug) {
 131              echo '<pre class="ixr_response">'.htmlspecialchars($debugContents)."\n</pre>\n\n";
 132          }
 133  
 134          // Now parse what we've got back
 135          $this->message = new IXR_Message($contents);
 136          if (!$this->message->parse()) {
 137              // XML error
 138              $this->error = new IXR_Error(-32700, 'parse error. not well formed');
 139              return false;
 140          }
 141  
 142          // Is the message a fault?
 143          if ($this->message->messageType == 'fault') {
 144              $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
 145              return false;
 146          }
 147  
 148          // Message must be OK
 149          return true;
 150      }
 151  
 152      function getResponse()
 153      {
 154          // methodResponses can only have one param - return that
 155          return $this->message->params[0];
 156      }
 157  
 158      function isError()
 159      {
 160          return (is_object($this->error));
 161      }
 162  
 163      function getErrorCode()
 164      {
 165          return $this->error->code;
 166      }
 167  
 168      function getErrorMessage()
 169      {
 170          return $this->error->message;
 171      }
 172  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1