Posted By

Pjotor on 03/24/10


Tagged

get json soap rest leoSr abstraction


Versions (?)

GETful SOAP to JSON


 / Published in: PHP
 

A really simple SOAP to JSON and back script, for when you just want to kick SOAP in the groin.

With jQuery you'd just do: $.getJSON('path/?function=myFunc&params={"param":"value"}&callback=?',function(json){alert(json);});

  1. <?php
  2.  
  3. /**
  4.  * Returns a JSON formatted response with optional callback-wrapping
  5.  * @param {String} function SOAP function to be called from the
  6.  * external WS
  7.  * @param {JSONObject} params Parameters to be sent to the SOAP function
  8.  * @param {bool} debug Debug switch (optional)
  9.  * @param {String} callback Callback function name (optional, defaults
  10.  * to @function)
  11.  *
  12.  * Example:
  13.  * ?function=mySOAPFunction&params={"SOAPParam":"value"}&
  14.  * callback=callbackFunction&debug=true
  15.  */
  16.  
  17. $SOAPFunction = (isset($_GET["function"])) ? $_GET["function"] : false;
  18. $SOAPParams = (isset($_GET["params"])) ? json_decode($_GET["params"]): false;
  19. $SOAPDebug = (isset($_GET["debug"])) ? true : false;
  20. $SOAPCallback = (isset($_GET["callback"])) ? $_GET["callback"] : $SOAPFunction;
  21.  
  22. if($SOAPFunction && $SOAPParams) {
  23.  
  24. $wsdl = "http://example.com/mySOAP.wsdl";
  25.  
  26. try {
  27. $client = new SoapClient($wsdl,array(
  28. "trace" => true,
  29. "exceptions" => $SOAPDebug));
  30.  
  31. $client -> call($SOAPFunction, $SOAPParams);
  32.  
  33. // NOTE:
  34. // in .NET you need a hack like this:
  35. /*
  36. switch($SOAPFunction) {
  37. case strtolower("mySOAPFunction"):
  38. $client -> mySOAPFunction($SOAPParams);
  39. break;
  40. default:
  41. die("no function found");
  42. }
  43. */
  44.  
  45. $lastReq = $client->__getLastRequest();
  46. $lastResp = $client->__getLastResponse();
  47.  
  48. // Comment to disable echoing of SOAP-envelope if the debug is on
  49. if($SOAPDebug){
  50. echo "\n/*\n";
  51. echo "Request: \n";
  52. echo $lastReq;
  53. echo "\n Response:\n";
  54. echo $lastResp ;
  55. echo "\n*/\n\n";
  56. }
  57.  
  58. print $SOAPCallback . "(" . xml2json(strtolower($lastResp)) .
  59. ");";
  60.  
  61. } catch (Exception $e) {
  62. print $SOAPCallback . "({\"soap:Envelope\":\"_c\":{\"soap:Body\":".
  63. "{\"_c\":{\"error\":{ \"{$SOAPFunction}\":{".
  64. "\"message\": \"" . $e->__toString() . "\"}".
  65. "}}}}});";
  66. }
  67. }
  68.  
  69.  
  70.  
  71. function xml2json(&$xml) {
  72. return json_encode(xml2ary($xml));
  73. }
  74.  
  75.  
  76.  
  77. // xml to array
  78. // author: leoSr
  79. // http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html
  80.  
  81. function xml2ary(&$string) {
  82. $parser = xml_parser_create();
  83. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  84. xml_parse_into_struct($parser, $string, $vals, $index);
  85. xml_parser_free($parser);
  86.  
  87. $mnary=array();
  88. $ary=&$mnary;
  89. foreach ($vals as $r) {
  90. $t=$r['tag'];
  91. if ($r['type']=='open') {
  92. if (isset($ary[$t])) {
  93. if (isset($ary[$t][0])) $ary[$t][]=array();
  94. else $ary[$t]=array($ary[$t], array());
  95. $cv=&$ary[$t][count($ary[$t])-1];
  96. } else $cv=&$ary[$t];
  97. if (isset($r['attributes'])) {
  98. foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
  99. $cv['_c']=array();
  100. $cv['_c']['_p']=&$ary;
  101. $ary=&$cv['_c'];
  102. } elseif ($r['type']=='complete') {
  103. if (isset($ary[$t])) { // same as open
  104. if (isset($ary[$t][0])) $ary[$t][]=array();
  105. else $ary[$t]=array($ary[$t], array());
  106. $cv=&$ary[$t][count($ary[$t])-1];
  107. } else $cv=&$ary[$t];
  108. if (isset($r['attributes'])) {
  109. foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
  110. $cv['_v']=(isset($r['value']) ? $r['value'] : '');
  111. } elseif ($r['type']=='close') {
  112. $ary=&$ary['_p'];
  113. }
  114. }
  115.  
  116. _del_p($mnary);
  117. return $mnary;
  118. }
  119.  
  120. // _Internal: Remove recursion in result array
  121. function _del_p(&$ary) {
  122. foreach ($ary as $k=>$v) {
  123. if ($k==='_p') unset($ary[$k]);
  124. elseif (is_array($ary[$k])) _del_p($ary[$k]);
  125. }
  126. }
  127. ?>

Report this snippet  

You need to login to post a comment.