説明なし

class-wp-http-ixr-client.php 3.4KB

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