[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WP_HTTP_IXR_Client
   4   *
   5   * @package WordPress
   6   * @since 3.1.0
   7   */
   8  class WP_HTTP_IXR_Client extends IXR_Client {
   9      public $scheme;
  10      /**
  11       * @var IXR_Error
  12       */
  13      public $error;
  14  
  15      /**
  16       * @param string $server
  17       * @param string|bool $path
  18       * @param int|bool $port
  19       * @param int $timeout
  20       */
  21  	public function __construct( $server, $path = false, $port = false, $timeout = 15 ) {
  22          if ( ! $path ) {
  23              // Assume we have been given a URL instead
  24              $bits         = parse_url( $server );
  25              $this->scheme = $bits['scheme'];
  26              $this->server = $bits['host'];
  27              $this->port   = isset( $bits['port'] ) ? $bits['port'] : $port;
  28              $this->path   = ! empty( $bits['path'] ) ? $bits['path'] : '/';
  29  
  30              // Make absolutely sure we have a path
  31              if ( ! $this->path ) {
  32                  $this->path = '/';
  33              }
  34  
  35              if ( ! empty( $bits['query'] ) ) {
  36                  $this->path .= '?' . $bits['query'];
  37              }
  38          } else {
  39              $this->scheme = 'http';
  40              $this->server = $server;
  41              $this->path   = $path;
  42              $this->port   = $port;
  43          }
  44          $this->useragent = 'The Incutio XML-RPC PHP Library';
  45          $this->timeout   = $timeout;
  46      }
  47  
  48      /**
  49       * @return bool
  50       */
  51  	public function query() {
  52          $args    = func_get_args();
  53          $method  = array_shift( $args );
  54          $request = new IXR_Request( $method, $args );
  55          $xml     = $request->getXml();
  56  
  57          $port = $this->port ? ":$this->port" : '';
  58          $url  = $this->scheme . '://' . $this->server . $port . $this->path;
  59          $args = array(
  60              'headers'    => array( 'Content-Type' => 'text/xml' ),
  61              'user-agent' => $this->useragent,
  62              'body'       => $xml,
  63          );
  64  
  65          // Merge Custom headers ala #8145
  66          foreach ( $this->headers as $header => $value ) {
  67              $args['headers'][ $header ] = $value;
  68          }
  69  
  70          /**
  71           * Filters the headers collection to be sent to the XML-RPC server.
  72           *
  73           * @since 4.4.0
  74           *
  75           * @param string[] $headers Associative array of headers to be sent.
  76           */
  77          $args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] );
  78  
  79          if ( $this->timeout !== false ) {
  80              $args['timeout'] = $this->timeout;
  81          }
  82  
  83          // Now send the request
  84          if ( $this->debug ) {
  85              echo '<pre class="ixr_request">' . htmlspecialchars( $xml ) . "\n</pre>\n\n";
  86          }
  87  
  88          $response = wp_remote_post( $url, $args );
  89  
  90          if ( is_wp_error( $response ) ) {
  91              $errno       = $response->get_error_code();
  92              $errorstr    = $response->get_error_message();
  93              $this->error = new IXR_Error( -32300, "transport error: $errno $errorstr" );
  94              return false;
  95          }
  96  
  97          if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
  98              $this->error = new IXR_Error( -32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')' );
  99              return false;
 100          }
 101  
 102          if ( $this->debug ) {
 103              echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
 104          }
 105  
 106          // Now parse what we've got back
 107          $this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
 108          if ( ! $this->message->parse() ) {
 109              // XML error
 110              $this->error = new IXR_Error( -32700, 'parse error. not well formed' );
 111              return false;
 112          }
 113  
 114          // Is the message a fault?
 115          if ( $this->message->messageType == 'fault' ) {
 116              $this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
 117              return false;
 118          }
 119  
 120          // Message must be OK
 121          return true;
 122      }
 123  }


Generated: Thu Jul 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1