/wp-includes/IXR/class-IXR-client.php

  1. <?php 
  2.  
  3. /** 
  4. * IXR_Client 
  5. * 
  6. * @package IXR 
  7. * @since 1.5.0 
  8. * 
  9. */ 
  10. class IXR_Client 
  11. var $server; 
  12. var $port; 
  13. var $path; 
  14. var $useragent; 
  15. var $response; 
  16. var $message = false; 
  17. var $debug = false; 
  18. var $timeout; 
  19. var $headers = array(); 
  20.  
  21. // Storage place for an error message 
  22. var $error = false; 
  23.  
  24. /** 
  25. * PHP5 constructor. 
  26. */ 
  27. function __construct( $server, $path = false, $port = 80, $timeout = 15 ) 
  28. if (!$path) { 
  29. // Assume we have been given a URL instead 
  30. $bits = parse_url($server); 
  31. $this->server = $bits['host']; 
  32. $this->port = isset($bits['port']) ? $bits['port'] : 80; 
  33. $this->path = isset($bits['path']) ? $bits['path'] : '/'; 
  34.  
  35. // Make absolutely sure we have a path 
  36. if (!$this->path) { 
  37. $this->path = '/'; 
  38.  
  39. if ( ! empty( $bits['query'] ) ) { 
  40. $this->path .= '?' . $bits['query']; 
  41. } else { 
  42. $this->server = $server; 
  43. $this->path = $path; 
  44. $this->port = $port; 
  45. $this->useragent = 'The Incutio XML-RPC PHP Library'; 
  46. $this->timeout = $timeout; 
  47.  
  48. /** 
  49. * PHP4 constructor. 
  50. */ 
  51. public function IXR_Client( $server, $path = false, $port = 80, $timeout = 15 ) { 
  52. self::__construct( $server, $path, $port, $timeout ); 
  53.  
  54. function query() 
  55. $args = func_get_args(); 
  56. $method = array_shift($args); 
  57. $request = new IXR_Request($method, $args); 
  58. $length = $request->getLength(); 
  59. $xml = $request->getXml(); 
  60. $r = "\r\n"; 
  61. $request = "POST {$this->path} HTTP/1.0$r"; 
  62.  
  63. // Merged from WP #8145 - allow custom headers 
  64. $this->headers['Host'] = $this->server; 
  65. $this->headers['Content-Type'] = 'text/xml'; 
  66. $this->headers['User-Agent'] = $this->useragent; 
  67. $this->headers['Content-Length']= $length; 
  68.  
  69. foreach( $this->headers as $header => $value ) { 
  70. $request .= "{$header}: {$value}{$r}"; 
  71. $request .= $r; 
  72.  
  73. $request .= $xml; 
  74.  
  75. // Now send the request 
  76. if ($this->debug) { 
  77. echo '<pre class="ixr_request">'.htmlspecialchars($request)."\n</pre>\n\n"; 
  78.  
  79. if ($this->timeout) { 
  80. $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout); 
  81. } else { 
  82. $fp = @fsockopen($this->server, $this->port, $errno, $errstr); 
  83. if (!$fp) { 
  84. $this->error = new IXR_Error(-32300, 'transport error - could not open socket'); 
  85. return false; 
  86. fputs($fp, $request); 
  87. $contents = ''; 
  88. $debugContents = ''; 
  89. $gotFirstLine = false; 
  90. $gettingHeaders = true; 
  91. while (!feof($fp)) { 
  92. $line = fgets($fp, 4096); 
  93. if (!$gotFirstLine) { 
  94. // Check line for '200' 
  95. if (strstr($line, '200') === false) { 
  96. $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); 
  97. return false; 
  98. $gotFirstLine = true; 
  99. if (trim($line) == '') { 
  100. $gettingHeaders = false; 
  101. if (!$gettingHeaders) { 
  102. // merged from WP #12559 - remove trim 
  103. $contents .= $line; 
  104. if ($this->debug) { 
  105. $debugContents .= $line; 
  106. if ($this->debug) { 
  107. echo '<pre class="ixr_response">'.htmlspecialchars($debugContents)."\n</pre>\n\n"; 
  108.  
  109. // Now parse what we've got back 
  110. $this->message = new IXR_Message($contents); 
  111. if (!$this->message->parse()) { 
  112. // XML error 
  113. $this->error = new IXR_Error(-32700, 'parse error. not well formed'); 
  114. return false; 
  115.  
  116. // Is the message a fault? 
  117. if ($this->message->messageType == 'fault') { 
  118. $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); 
  119. return false; 
  120.  
  121. // Message must be OK 
  122. return true; 
  123.  
  124. function getResponse() 
  125. // methodResponses can only have one param - return that 
  126. return $this->message->params[0]; 
  127.  
  128. function isError() 
  129. return (is_object($this->error)); 
  130.  
  131. function getErrorCode() 
  132. return $this->error->code; 
  133.  
  134. function getErrorMessage() 
  135. return $this->error->message; 
.