There is an example of class to show you how to write code to consume a SOAP based web service.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/** * Description of mySC * * @author houssem.blibech */ class mySC { private $_objSC; private $_wsdl; /** * * @param type $wsdl */ function __construct($wsdl) { try { $options = array("trace" => 1, "exceptions" => 1); $this->_wsdl = $wsdl; $this->_objSC = new SoapClient($this->_wsdl, $options); watchdog('WS', 'create a new SoapClient object for [%name] service.', array('%name' => $wsdl)); } catch (Exception $e) { echo '<pre>'; echo 'Exception : ' . $e->__toString() . '<br/>'; echo '</pre>'; watchdog('WS', 'Cannot create a new SoapClient object for [%name] service.', array('%name' => $wsdl)); } } /** * Execute web service methode * @param type $method * @param type $params * @return type */ public function RequestSW($method, $params = array()) { try { $value = $this->_objSC->__soapCall($method, array($params)); watchdog('WS', 'Call the [%name] method with this parameters [%params].', array('%name' => $method, '%params' => implode('#', $params))); $this->LogMyWS($this->_objSC); } catch (Exception $e) { echo '<pre>'; echo 'Exception : ' . $e->__toString() . '<br/>'; echo '</pre>'; watchdog('WS', 'Cannot call the [%name] method with this parameters [%params].', array('%name' => $method, '%params' => var_dump($params))); $this->LogMyWS($this->_objSC); } return $value; } /** * */ public function GetFunctions(){ $functions = $this->_objSC->__getFunctions(); echo "<pre>"; echo "Liste des fonctions : <br />"; echo var_dump($functions); echo "</pre>"; } /** * Log des Request / Response des WS * * @param Object $SOAPObject */ private function LogMyWS($SOAPObject){ // Log WS $output = "RequestHeaders : "; $output .= $SOAPObject->__getLastRequestHeaders(); $output .= "<br />"; $output .= "\n"; $output .= "Request : "; $output .= $SOAPObject->__getLastRequest(); $output .= "<br />"; $output .= "\n"; $output .= "ResponseHeaders : "; $output .= $SOAPObject->__getLastResponseHeaders(); $output .= "<br />"; $output .= "\n"; $output .= "Response : "; $output .= $SOAPObject->__getLastResponse(); watchdog('WS', '%log', array('%log' => $output)); } } |
In the code below a demo of how to instanciate this class and call the RequestSW method :
1 2 3 4 5 6 |
$wsdl_path = "url_to_wsdl"; $oMySC = new mySC($wsdl_path); $params= array(val1, val2, val3, val4); $my_soap_client_obj = $oMySC->RequestSW('method_name', $params); |
No comments yet.