PHP鏈接WEB SERVICE類代碼實例:
- //通訊類開始
- class webserv
- {
- //webservice 地址參數
- var $webadd = array(
- "publ" => "client.asmx?wsdl",
- "client" => "Client.asmx?wsdl",
- "bann" => "");
- //調用webservice
- function soap($class,$function,$par = array()){
- //鏈接
- web service
- $client = new SoapClient($this->webadd[$class]);
- $require = $client->$function($par);
- //調用信息debug
- if (is_soap_fault($require)) {
- echo "遠程接口".$function."調用失敗!";
- exit();
- }
- //獲取返回值
- $function_result = $function."Result";
- $str = $require->$function_result;
- //解析XML
- $row = @simplexml_load_string( $str );
- //如果不能用XML解析直接返回
- if ($row == false) {
- if (is_string($str)){
- return $str;
- } else {
- $require = (get_object_vars($str));
- $str = $require['string'];
- return $str;
- }
- }
- //結果轉換為數組
- $row_arr = get_object_vars($row);
- print_r($row_arr);exit();
- $row_return = $row_arr['Table'];
- //判斷是不是多反饋記錄
- if (is_array($row_arr['Table'])) {
- foreach ($row_return as $key => $vlaues){
- $rows[$key] = get_object_vars($vlaues);
- }
- } else {
- $rows[0] = get_object_vars($row_arr['Table']);
- }
- //通訊記錄
- //$this->soap_log($class,$function,$par,$rows);
- return $rows;
- }
- //接口通訊記錄
- function soap_log($class, $function, $in, $out){
- $log = date("Y-m-d H:i:s");
- $log .= " ".$class."->".$function." input:";
- $input = "";
- if ($in != array()) {
- foreach ($in as $key => $vlaue){
- $input .= $key."=".$vlaue;
- }
- $log .= $input." output:".var_export($rows)."n";
- }
- }
- }
以上是通信類的內容。用法很簡單
include 一下PHP鏈接WEB SERVICE類文件
- < ?
- include "./webserv.class.php";
- $webserv = new webserv;
- //這裡是調用方法給傳遞的參數
- $a = array("aa"=>"aaaa");
- print_r($webser->soap("function_name",$a));
- ?>
以上就是PHP鏈接WEB SERVICE類的相關使用方法。