详细解读PHP链接WEB SERVICE类_PHP教程
程序员文章站
2022-04-28 18:37:00
...
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类的相关使用方法。