mickmackusa

XPlan Search QueryXML Issue

Jun 14th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. <style>
  2. pre {max-height: 400px; overflow: auto; background-color: #EEEEEE;}
  3. div {border: solid 2px black;}
  4. .error {border: solid 1px red;}
  5. .notice {border: solid 1px blue;}
  6. .success { border: solid 1px green;}
  7. </style>
  8. <?php
  9.  
  10. class XMLCurler
  11. {
  12. public $username = '[redacted]';
  13. public $password = '[redacted]';
  14. public $url = 'https://amp.xplan.iress.com.au/RPC2/';
  15. public $ch; // the curl handle
  16. public $token;
  17. public $results;
  18.  
  19. public function __construct() {
  20. if ($this->connect()) {
  21. if ($this->login()) {
  22. echo "<div class=\"success\">Successful Connection & Login. Token: {$this->token}</div>";
  23. }
  24. }
  25. }
  26.  
  27. public function __destruct() {
  28. if ($this->ch) {
  29. $this->disconnect();
  30. }
  31. }
  32.  
  33. public function connect() {
  34. if (!$this->ch = curl_init($this->url)) { // generate curl handle
  35. echo "<div class=\"error\">CURL Error While Connecting (check url)";
  36. return false;
  37. }
  38. return true;
  39. }
  40.  
  41. public function disconnect() {
  42. curl_close($this->ch);
  43. }
  44.  
  45. public function processResponse($response) {
  46. if (!$response) {
  47. echo "<div class=\"error\">CURL Error While Attempting to Login - No XML token string<br><b>" , curl_error($this->ch) , "</b></div>";
  48. return false;
  49. }
  50. $decoded = xmlrpc_decode($response);
  51. if (is_array($decoded) && xmlrpc_is_fault($decoded)) {
  52. echo "<div class=\"error\">Error Response: {$decoded['faultString']} ({$decoded['faultCode']})</div>";
  53. return false;
  54. }
  55. return $decoded;
  56. }
  57.  
  58. public function login() {
  59. $postfields = xmlrpc_encode_request('edai.Login', array($this->username, $this->password)); // package as xml
  60. curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  61. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  62. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postfields);
  63. curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0); // not advised, I need to find out how to avoid this
  64. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // not advised, I need to find out how to avoid this
  65.  
  66. if (!$token = $this->processResponse(curl_exec($this->ch))) {
  67. return false;
  68. }
  69. if (!preg_match("~^[\w+]{20}$~", $token)) {
  70. echo "<div class=\"error\">Invalid/Unexpected Token Generated<br><b>$token</b>";
  71. return false;
  72. }
  73. $this->token = $token; // cache the valid token
  74. return true;
  75. }
  76.  
  77. public function listChildren($path) {
  78. $method = "edai.ListChildren";
  79. $request = xmlrpc_encode_request($method, array($this->token, $path));
  80.  
  81. echo "<div class=\"notice\">XMLRPC Encoded Request (for $method): <pre>" , htmlentities($request) , "</pre></div>";
  82.  
  83. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
  84. if (!$results = $this->processResponse(curl_exec($this->ch))) {
  85. return false;
  86. }
  87. $this->results = $results; // cache the valid results
  88. return true;
  89. }
  90.  
  91.  
  92. public function search($basepath, $queryxml) {
  93. $method = "edai.Search";
  94.  
  95. /** Desperate/Manual xml construction ...
  96. * $xml = new DOMDocument("1.0", "utf-8");
  97. * $xml->appendChild($methodCall = $xml->createElement("methodCall"));
  98. * $methodCall->appendChild($methodName = $xml->createElement("methodName"));
  99. * $methodCall->appendChild($params = $xml->createElement("params"));
  100.  
  101. * $params->appendChild($param1 = $xml->createElement("param"));
  102. * $param1->appendChild($value1 = $xml->createElement("value"));
  103. * $value1->appendChild($string1 = $xml->createElement("string"));
  104.  
  105. * $params->appendChild($param2 = $xml->createElement("param"));
  106. * $param2->appendChild($value2 = $xml->createElement("value"));
  107. * $value2->appendChild($string2 = $xml->createElement("string"));
  108.  
  109. * $params->appendChild($param3 = $xml->createElement("param"));
  110. * $param3->appendChild($value3 = $xml->createElement("value"));
  111. * $value3->appendChild($string3 = $xml->createElement("string"));
  112. * $string3->appendChild($EntitySearch = $xml->createElement("EntitySearch"));
  113. * $EntitySearch->appendChild($SearchResult1 = $xml->createElement("SearchResult"));
  114. * $SearchResult1->setAttribute("field", "first_name");
  115.  
  116. * $EntitySearch->appendChild($SearchResult2 = $xml->createElement("SearchResult"));
  117. * $SearchResult2->setAttribute('field', "last_name");
  118.  
  119. * $EntitySearch->appendChild($SearchQuick = $xml->createElement("SearchQuick"));
  120. * $SearchQuick->appendChild($s = $xml->createElement("s"));
  121.  
  122. * $xpath = new DOMXPath($xml);
  123. * $result1 = $xpath->query("//methodName");
  124. * $result1->item(0)->nodeValue = $method;
  125.  
  126. * $result2 = $xpath->query("//params/param[1]/value/string");
  127. * $result2->item(0)->nodeValue = $this->token;
  128. * $result3 = $xpath->query("//params/param[2]/value/string");
  129. * $result3->item(0)->nodeValue = "entitymgr/client";
  130.  
  131. * $result4 = $xpath->query("//SearchQuick/s");
  132. * $result4->item(0)->nodeValue = "last_name:Smith";
  133.  
  134. * $xml->formatOutput = true;
  135. * $request = $xml->saveXML();
  136. */
  137.  
  138. /** Desperately attempted passing array ...
  139. * $queryarray = array(
  140. * "EntitySearch" => array(
  141. * array(
  142. * "SearchResult" => array(
  143. * "@attr" => array(
  144. * "field" => "first_name"
  145. * )
  146. * )
  147. * ),
  148. * array(
  149. * "SearchResult" => array(
  150. * "@attr" => array(
  151. * "field" => "last_name"
  152. * )
  153. * )
  154. * ),
  155. * array(
  156. * "SearchQuick" => array(
  157. * "s" => "last_name:Smith"
  158. * )
  159. * )
  160. * )
  161. * );
  162. */
  163.  
  164. $request = xmlrpc_encode_request($method, array($this->token, $basepath, $queryxml)); // this mutates the nest $queryxml string
  165. // Causes:
  166. //Error Response: UNKNOWN(CORBA.UNKNOWN(omniORB.UNKNOWN_PythonException, CORBA.COMPLETED_MAYBE)) (-32505)
  167. //$request = html_entity_decode($request); // repair encoded entities
  168. //$request = preg_replace('~(?:>\K\s+)|(?:\s+(?=<))~', '', $request); // strip every whitespace character between tags (hack)
  169. // Causes:
  170. // Error Response: ExpatError(syntax error: line 1, column 0 (byte 0)) (-32505)
  171. echo "<div class=\"notice\">XMLRPC Encoded Request (for $method): <pre>" , htmlentities($request) , "</pre></div>";
  172.  
  173. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
  174. if (!$results = $this->processResponse(curl_exec($this->ch))) {
  175. return false;
  176. }
  177. $this->results = $results; // cache the valid results
  178. return true;
  179. }
  180. }
  181.  
  182. $XC = new XMLCurler();
  183.  
  184. /* edai.ListChildren works as desired/expected */
  185. $path = "/entitymgr/client";
  186. if ($XC->listChildren($path)) {
  187. echo "<div>List of Children Successful.<pre>";
  188. var_export($XC->results);
  189. echo "</pre></div>";
  190. }
  191.  
  192. /* edai.Search does not work */
  193. $basepath = "entitymgr/client";
  194. $queryxml = <<<XML
  195. <EntitySearch>
  196. <SearchResult field="first_name"/>
  197. <SearchResult field="last_name"/>
  198. <SearchQuick><s>last_name:Smith</s></SearchQuick>
  199. </EntitySearch>
  200. XML;
  201. if ($XC->search($basepath, $queryxml)) {
  202. echo "<div>Search Successful.<pre>";
  203. var_export($XC->results);
  204. echo "</pre></div>";
  205. }
Advertisement
Add Comment
Please, Sign In to add comment