[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Wed Oct 7 08:20:33 2026 Cross-referenced by PHPXref