/Web/wp-includes/class-wp-http-ixr-client.php

https://bitbucket.org/jimjenkins5/blog · PHP · 92 lines · 63 code · 14 blank · 15 comment · 12 complexity · c6705622e18cf5272f7bdaf34272f375 MD5 · raw file

  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. function __construct($server, $path = false, $port = false, $timeout = 15) {
  11. if ( ! $path ) {
  12. // Assume we have been given a URL instead
  13. $bits = parse_url($server);
  14. $this->scheme = $bits['scheme'];
  15. $this->server = $bits['host'];
  16. $this->port = isset($bits['port']) ? $bits['port'] : $port;
  17. $this->path = !empty($bits['path']) ? $bits['path'] : '/';
  18. // Make absolutely sure we have a path
  19. if ( ! $this->path )
  20. $this->path = '/';
  21. } else {
  22. $this->scheme = 'http';
  23. $this->server = $server;
  24. $this->path = $path;
  25. $this->port = $port;
  26. }
  27. $this->useragent = 'The Incutio XML-RPC PHP Library';
  28. $this->timeout = $timeout;
  29. }
  30. function query() {
  31. $args = func_get_args();
  32. $method = array_shift($args);
  33. $request = new IXR_Request($method, $args);
  34. $xml = $request->getXml();
  35. $port = $this->port ? ":$this->port" : '';
  36. $url = $this->scheme . '://' . $this->server . $port . $this->path;
  37. $args = array(
  38. 'headers' => array('Content-Type' => 'text/xml'),
  39. 'user-agent' => $this->useragent,
  40. 'body' => $xml,
  41. );
  42. // Merge Custom headers ala #8145
  43. foreach ( $this->headers as $header => $value )
  44. $args['headers'][$header] = $value;
  45. if ( $this->timeout !== false )
  46. $args['timeout'] = $this->timeout;
  47. // Now send the request
  48. if ( $this->debug )
  49. echo '<pre class="ixr_request">' . htmlspecialchars($xml) . "\n</pre>\n\n";
  50. $response = wp_remote_post($url, $args);
  51. if ( is_wp_error($response) ) {
  52. $errno = $response->get_error_code();
  53. $errorstr = $response->get_error_message();
  54. $this->error = new IXR_Error(-32300, "transport error: $errno $errorstr");
  55. return false;
  56. }
  57. if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
  58. $this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')');
  59. return false;
  60. }
  61. if ( $this->debug )
  62. echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
  63. // Now parse what we've got back
  64. $this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
  65. if ( ! $this->message->parse() ) {
  66. // XML error
  67. $this->error = new IXR_Error(-32700, 'parse error. not well formed');
  68. return false;
  69. }
  70. // Is the message a fault?
  71. if ( $this->message->messageType == 'fault' ) {
  72. $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
  73. return false;
  74. }
  75. // Message must be OK
  76. return true;
  77. }
  78. }