[ 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|false $path
  18       * @param int|false    $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       * @since 3.1.0
  50       * @since 5.5.0 Formalized the existing `...$args` parameter by adding it
  51       *              to the function signature.
  52       *
  53       * @return bool
  54       */
  55  	public function query( ...$args ) {
  56          $method  = array_shift( $args );
  57          $request = new IXR_Request( $method, $args );
  58          $xml     = $request->getXml();
  59  
  60          $port = $this->port ? ":$this->port" : '';
  61          $url  = $this->scheme . '://' . $this->server . $port . $this->path;
  62          $args = array(
  63              'headers'    => array( 'Content-Type' => 'text/xml' ),
  64              'user-agent' => $this->useragent,
  65              'body'       => $xml,
  66          );
  67  
  68          // Merge Custom headers ala #8145.
  69          foreach ( $this->headers as $header => $value ) {
  70              $args['headers'][ $header ] = $value;
  71          }
  72  
  73          /**
  74           * Filters the headers collection to be sent to the XML-RPC server.
  75           *
  76           * @since 4.4.0
  77           *
  78           * @param string[] $headers Associative array of headers to be sent.
  79           */
  80          $args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] );
  81  
  82          if ( false !== $this->timeout ) {
  83              $args['timeout'] = $this->timeout;
  84          }
  85  
  86          // Now send the request.
  87          if ( $this->debug ) {
  88              echo '<pre class="ixr_request">' . htmlspecialchars( $xml ) . "\n</pre>\n\n";
  89          }
  90  
  91          $response = wp_remote_post( $url, $args );
  92  
  93          if ( is_wp_error( $response ) ) {
  94              $errno       = $response->get_error_code();
  95              $errorstr    = $response->get_error_message();
  96              $this->error = new IXR_Error( -32300, "transport error: $errno $errorstr" );
  97              return false;
  98          }
  99  
 100          if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
 101              $this->error = new IXR_Error( -32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')' );
 102              return false;
 103          }
 104  
 105          if ( $this->debug ) {
 106              echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
 107          }
 108  
 109          // Now parse what we've got back.
 110          $this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
 111          if ( ! $this->message->parse() ) {
 112              // XML error.
 113              $this->error = new IXR_Error( -32700, 'parse error. not well formed' );
 114              return false;
 115          }
 116  
 117          // Is the message a fault?
 118          if ( 'fault' === $this->message->messageType ) {
 119              $this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
 120              return false;
 121          }
 122  
 123          // Message must be OK.
 124          return true;
 125      }
 126  }


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1