You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

125 lines
3.2 KiB

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