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

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