/ 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¶ms={"param":"value"}&callback=?',function(json){alert(json);});
Expand |
Embed | Plain Text
<?php /** * Returns a JSON formatted response with optional callback-wrapping * @param {String} function SOAP function to be called from the * external WS * @param {JSONObject} params Parameters to be sent to the SOAP function * @param {bool} debug Debug switch (optional) * @param {String} callback Callback function name (optional, defaults * to @function) * * Example: * ?function=mySOAPFunction¶ms={"SOAPParam":"value"}& * callback=callbackFunction&debug=true */ if($SOAPFunction && $SOAPParams) { $wsdl = "http://example.com/mySOAP.wsdl"; try { "trace" => true, "exceptions" => $SOAPDebug)); $client -> call($SOAPFunction, $SOAPParams); // NOTE: // in .NET you need a hack like this: /* switch($SOAPFunction) { case strtolower("mySOAPFunction"): $client -> mySOAPFunction($SOAPParams); break; default: die("no function found"); } */ $lastReq = $client->__getLastRequest(); $lastResp = $client->__getLastResponse(); // Comment to disable echoing of SOAP-envelope if the debug is on if($SOAPDebug){ echo "\n/*\n"; echo "Request: \n"; echo $lastReq; echo "\n Response:\n"; echo $lastResp ; echo "\n*/\n\n"; } ");"; } catch (Exception $e) { "{\"_c\":{\"error\":{ \"{$SOAPFunction}\":{". "\"message\": \"" . $e->__toString() . "\"}". "}}}}});"; } } function xml2json(&$xml) { return json_encode(xml2ary($xml)); } // xml to array // author: leoSr // http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html function xml2ary(&$string) { $ary=&$mnary; foreach ($vals as $r) { $t=$r['tag']; if ($r['type']=='open') { } else $cv=&$ary[$t]; foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} $cv['_c']['_p']=&$ary; $ary=&$cv['_c']; } elseif ($r['type']=='complete') { } else $cv=&$ary[$t]; foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} } elseif ($r['type']=='close') { $ary=&$ary['_p']; } } _del_p($mnary); return $mnary; } // _Internal: Remove recursion in result array function _del_p(&$ary) { foreach ($ary as $k=>$v) { } } ?>
You need to login to post a comment.
