IXR_Client

IXR_Client.

Defined (1)

The class is defined in the following location(s).

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