NXS_XMLRPC_Client

NXS_XMLRPC_Client.

Defined (1)

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

/inc-cl/apis/xmlrpc-client.php  
  1. class NXS_XMLRPC_Client 
  2. var $server; 
  3. var $port; 
  4. var $path; 
  5. var $scheme; 
  6. var $useragent; 
  7. var $response; 
  8. var $message = false; 
  9. var $debug = false; 
  10. var $timeout; 
  11.  
  12. // Storage place for an error message 
  13. var $error = false; 
  14.  
  15. function NXS_XMLRPC_Client($server, $path = false, $port = 80, $timeout = 25) 
  16. if (!$path) { 
  17. // Assume we have been given a URL instead 
  18. $bits = parse_url($server); 
  19. $this->server = $bits['host']; 
  20. $this->scheme = isset($bits['scheme']) ? $bits['scheme'] : 'http'; 
  21. $this->port = isset($bits['port']) ? $bits['port'] : ($bits['scheme']=='https'?443:80); 
  22. $this->path = isset($bits['path']) ? $bits['path'] : '/'; 
  23.  
  24. // Make absolutely sure we have a path 
  25. if (!$this->path) { 
  26. $this->path = '/'; 
  27. } else { 
  28. $this->server = $server; 
  29. $this->path = $path; 
  30. $this->port = $port; 
  31. $this->useragent = 'The Incutio XML-RPC PHP Library'; 
  32. $this->timeout = $timeout; 
  33.  
  34. function queryFS() 
  35. $args = func_get_args(); 
  36. $method = array_shift($args); 
  37. $request = new NXS_XMLRPC_Request($method, $args); 
  38. $length = $request->getLength(); 
  39. $xml = $request->getXml(); 
  40. $r = "\r\n"; 
  41. $request = "POST {$this->path} HTTP/1.0$r"; 
  42.  
  43. // Merged from WP #8145 - allow custom headers 
  44. $this->headers['Host'] = $this->server; 
  45. $this->headers['Content-Type'] = 'text/xml'; 
  46. $this->headers['User-Agent'] = $this->useragent; 
  47. $this->headers['Content-Length']= $length; 
  48.  
  49. foreach( $this->headers as $header => $value ) { 
  50. $request .= "{$header}: {$value}{$r}"; 
  51. $request .= $r; 
  52.  
  53. $request .= $xml; 
  54.  
  55. // Now send the request 
  56. if ($this->debug) { 
  57. echo '<pre class="NXS_XMLRPC_request">'.htmlspecialchars($request)."\n</pre>\n\n"; 
  58.  
  59. if ($this->timeout) { 
  60. $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout); 
  61. } else { 
  62. $fp = @fsockopen($this->server, $this->port, $errno, $errstr); 
  63. if (!$fp) { 
  64. $this->error = new NXS_XMLRPC_Error(-32300, 'transport error - could not open socket'); 
  65. return false; 
  66. fputs($fp, $request); 
  67. $contents = ''; 
  68. $debugContents = ''; 
  69. $gotFirstLine = false; 
  70. $gettingHeaders = true; 
  71. while (!feof($fp)) { 
  72. $line = fgets($fp, 4096); 
  73. if (!$gotFirstLine) { 
  74. // Check line for '200' 
  75. if (strstr($line, '200') === false) { 
  76. $this->error = new NXS_XMLRPC_Error(-32300, 'transport error - HTTP status code was not 200'); 
  77. return false; 
  78. $gotFirstLine = true; 
  79. if (trim($line) == '') { 
  80. $gettingHeaders = false; 
  81. if (!$gettingHeaders) { 
  82. // merged from WP #12559 - remove trim 
  83. $contents .= $line; 
  84. if ($this->debug) { 
  85. $debugContents .= $line; 
  86. if ($this->debug) { 
  87. echo '<pre class="NXS_XMLRPC_response">'.htmlspecialchars($debugContents)."\n</pre>\n\n"; 
  88.  
  89. // Now parse what we've got back 
  90. $this->message = new NXS_XMLRPC_Message($contents); 
  91. if (!$this->message->parse()) { 
  92. // XML error 
  93. $this->error = new NXS_XMLRPC_Error(-32700, 'parse error. not well formed'); 
  94. return false; 
  95.  
  96. // Is the message a fault? 
  97. if ($this->message->messageType == 'fault') { 
  98. $this->error = new NXS_XMLRPC_Error($this->message->faultCode, $this->message->faultString); 
  99. return false; 
  100.  
  101. // Message must be OK 
  102. return true; 
  103.  
  104. /** 
  105. * Set the query to send to the XML-RPC Server 
  106. * @since 0.1.0 
  107. */ 
  108. function query() 
  109. $args = func_get_args(); 
  110. $method = array_shift($args); 
  111. $request = new NXS_XMLRPC_Request($method, $args); 
  112. $length = $request->getLength(); 
  113. $xml = $request->getXml(); 
  114.  
  115. if ($this->debug) { 
  116. echo '<pre>'.htmlspecialchars($xml)."\n</pre>\n\n"; 
  117.  
  118. //This is where we deviate from the normal query() 
  119. //Rather than open a normal sock, we will actually use the cURL 
  120. //extensions to make the calls, and handle the SSL stuff. 
  121.  
  122. //Since 04Aug2004 (0.1.3) - Need to include the port (duh...) 
  123. //Since 06Oct2004 (0.1.4) - Need to include the colon!!! 
  124. // (I swear I've fixed this before... ESP in live... But anyhu...) 
  125. $curl=curl_init($this->scheme.'://' . $this->server . ':' . $this->port . $this->path); 
  126. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  127.  
  128. curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.44 Safari/537.36");  
  129.  
  130. //Since 23Jun2004 (0.1.2) - Made timeout a class field 
  131. curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); 
  132.  
  133. if ($this->debug) { 
  134. curl_setopt($curl, CURLOPT_VERBOSE, 1); 
  135.  
  136.  
  137. curl_setopt($curl, CURLOPT_HEADER, 1); 
  138. curl_setopt($curl, CURLOPT_POST, 1); 
  139. curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); 
  140. curl_setopt($curl, CURLOPT_PORT, $this->port); 
  141. curl_setopt($curl, CURLOPT_HTTPHEADER, array( 
  142. "Content-Type: text/xml",  
  143. "Content-length: {$length}")); 
  144.  
  145. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
  146. global $nxs_skipSSLCheck; if ($nxs_skipSSLCheck===true) curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  147.  
  148. // Call cURL to do it's stuff and return us the content 
  149. $contents = curl_exec($curl); $err = curl_errno($curl); $errmsg = curl_error($curl);  
  150. curl_close($curl); 
  151.  
  152. // Check for 200 Code in $contents 
  153. if (!strstr($contents, '200 OK')) { 
  154. //There was no "200 OK" returned - we failed 
  155. $this->error = new NXS_XMLRPC_Error(-32300, 'transport error - HTTP status code was not 200 - '.$err.' - '.$errmsg.' | '.print_r($contents, true)); 
  156. return false; 
  157.  
  158. if ($this->debug) { 
  159. echo '<pre>'.htmlspecialchars($contents)."\n</pre>\n\n"; 
  160. // Now parse what we've got back 
  161. // Since 20Jun2004 (0.1.1) - We need to remove the headers first 
  162. // Why I have only just found this, I will never know... 
  163. // So, remove everything before the first < 
  164. $contents = substr($contents, strpos($contents, '<')); 
  165.  
  166. $this->message = new NXS_XMLRPC_Message($contents); 
  167. if (!$this->message->parse()) { 
  168. // XML error 
  169. $this->error = new NXS_XMLRPC_Error(-32700, 'parse error. not well formed'); 
  170. return false; 
  171. // Is the message a fault? 
  172. if ($this->message->messageType == 'fault') { 
  173. $this->error = new NXS_XMLRPC_Error($this->message->faultCode, $this->message->faultString); 
  174. return false; 
  175.  
  176. // Message must be OK 
  177. return true; 
  178.  
  179. function getResponse() 
  180. // methodResponses can only have one param - return that 
  181. return $this->message->params[0]; 
  182.  
  183. function isError() 
  184. return (is_object($this->error)); 
  185.  
  186. function getErrorCode() 
  187. return $this->error->code; 
  188.  
  189. function getErrorMessage() 
  190. return $this->error->message;