开发者社区> 技术小胖子> 正文

PHP根据wsdl生成调用soap webservice代码

简介:
+关注继续查看

PHP根据wsdl生成调用soap webservice代码


  1. <?php 
  2. class soap_code_create { 
  3.     public $wsdl
  4.     public $root_dir
  5.     private $soap
  6.     private $class_pre
  7.     private $php_pre_separation
  8.     private $php_end_separation
  9.     private $br_separation
  10.      
  11.     public function __construct($wsdl$root_dir$class_pre) { 
  12.         $this->wsdl = $wsdl
  13.         $this->root_dir = $root_dir
  14.         $this->soap = new SoapClient ( $wsdl ); 
  15.         $this->class_pre = $class_pre
  16.         $this->php_pre_separation = "<?php "
  17.         $this->php_end_separation = "?>"
  18.         $this->br_separation = "\n"
  19.     } 
  20.     public function getFunctions() { 
  21.         return $this->soap->__getFunctions (); 
  22.     } 
  23.     public function getTypes() { 
  24.         return $this->soap->__getTypes (); 
  25.     } 
  26.     public function write_file($file_path$content) { 
  27.         $handle = fopen ( $file_path'a' ); 
  28.         fwrite ( $handle$content ); 
  29.         fclose ( $handle ); 
  30.     } 
  31.     public function create_construct_pre() { 
  32.         return "public function __construct(\$parmas){" . $this->br_separation; 
  33.     } 
  34.     public function create_construct_end() { 
  35.         return "}" . $this->br_separation; 
  36.     } 
  37.     public function create_base_class() { 
  38.         $types = $this->getTypes (); 
  39.         if (sizeof ( $types ) > 0) { 
  40.             foreach ( $types as $type ) { 
  41.                 $type_array = split ( "\n"$type ); 
  42.                 $x_size = sizeof ( $type_array ); 
  43.                 if ($x_size > 0) { 
  44.                     $vars = array (); 
  45.                     $class_content_string = ""
  46.                     $class_name = ""
  47.                     foreach ( $type_array as $k_x => $x_value ) { 
  48.                         if ($k_x == 0) { 
  49.                         //处理获取文件名 
  50.                         $class_name = str_ireplace ( " {"""$x_value ); 
  51.                         $class_name = str_ireplace ( "struct """$class_name ); 
  52.                         $class_name = str_ireplace ( " """$class_name ); 
  53.                         //生成初始字符串 
  54.                         if($this->class_pre == ""){ 
  55.                         $class_name = $class_name
  56.                         }else { 
  57.                         $class_name = $this->class_pre . "_" . $class_name
  58.                         } 
  59.  
  60.                         } elseif ($k_x == ($x_size - 1)) { 
  61.                         //处理}没有任何操作 
  62.  
  63.                         } else { 
  64.                         $body = str_ireplace ( ";"""$x_value ); 
  65.                         $body = $this->cut_first_letter ( $body" " ); 
  66.                         $body = $this->cut_end_letter ( $body" " ); 
  67.                         $var = split ( " "$body ); 
  68.                         $vars [] = $var
  69.                         } 
  70.                     } 
  71.                     $class_content_string .= $this->php_pre_separation; 
  72.                     $class_content_string .= $this->br_separation; 
  73.                     $class_content_string .= "class " . $class_name . " { "
  74.                     $class_content_string .= $this->br_separation; 
  75.                     $content_var = ""
  76.                     $content_fun = ""
  77.                     if (sizeof ( $vars ) > 0) { 
  78.                     foreach ( $vars as $v ) { 
  79.                     $content_var .= "public $" . $v [1] . "; " . $this->br_separation; 
  80.                     } 
  81.                     foreach ( $vars as $v2 ) { 
  82.                     $content_fun .= "$" . "this->" . $v2 [1] . " = " . "$" . "parmas['" . $v2 [1] . "'];"
  83.                     $content_fun .= $this->br_separation; 
  84.                     } 
  85.                     } 
  86.                     $class_content_string .= $content_var
  87.                     $class_content_string .= $this->create_construct_pre (); 
  88.                     $class_content_string .= $content_fun
  89.                     $class_content_string .= $this->create_construct_end (); 
  90.                     $class_content_string .= "}" . $this->br_separation; 
  91.                     $class_content_string .= $this->php_end_separation; 
  92.                     $class_file_name = $class_name . ".class.php"
  93.                     $file_path = $this->root_dir . "/" . $class_file_name
  94.                     $this->write_file ( $file_path$class_content_string ); 
  95.                 } 
  96.  
  97.             } 
  98.         } 
  99.     } 
  100.     /** 
  101.     * 去除字符串前的特定字符 
  102.     */ 
  103.     public function cut_first_letter($letters$split) { 
  104.         $strlen = strlen ( $letters ); 
  105.         $first_flag = false; 
  106.         $letters_cut_first = ""
  107.         for($i = 0; $i < $strlen$i ++) { 
  108.             if ($first_flag) { 
  109.             continue
  110.             } 
  111.             $current_letter = substr ( $letters$i, 1 ); 
  112.             $next_i = ($i == $strlen - 1) ? $strlen - 1 : $i + 1; 
  113.             $next_letter = substr ( $letters$next_i, 1 ); 
  114.             if ($current_letter != $split) { 
  115.             $first_flag = true; 
  116.             $letters_cut_first = $letters
  117.             } 
  118.             if ($current_letter == $split && $next_letter != $split) { 
  119.             $first_flag = true; 
  120.             $letters_cut_first = substr ( $letters$next_i$strlen - $i ); 
  121.             } 
  122.         } 
  123.         return $letters_cut_first
  124.     } 
  125.     /** 
  126.     * 去除字符串尾部的指定字符 
  127.     */ 
  128.     public function cut_end_letter($letters$split) { 
  129.         $strlen = strlen ( $letters ); 
  130.         $letters_cut_end = ""
  131.         $end_flag = false; 
  132.         for($j = $strlen$j > 0; $j --) { 
  133.             if ($end_flag) { 
  134.             continue
  135.             } 
  136.             $end_letter = substr ( $letters$j, 1 ); 
  137.             $end_letter_pre = substr ( $letters$j - 1, 1 ); 
  138.             if ($end_letter != $split) { 
  139.             $end_flag = true; 
  140.             $letters_cut_end = $letters
  141.             } 
  142.             if ($end_letter == $split && $end_letter_pre != $split) { 
  143.             $end_flag = true; 
  144.             $letters_cut_end = substr ( $letters, 0, $j ); 
  145.             } 
  146.         } 
  147.         return $letters_cut_end
  148.     } 
  149.  
  150. ?> 

 



      本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/925185,如需转载请自行联系原作者






版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
php 如何利用 soap调用.Net的WebService asmx文件
原文:php 如何利用 soap调用.Net的WebService asmx文件 最近,帮一个同行测试用.net写的WebService接口,C#调用通过,现在需要测试一下php版本对它的调用,经过各种探索, 相关的PHP调用webservice的过程如下: 1.
1296 0
php调用webservice的几种方法
原文:php调用webservice的几种方法 1.WSDL模式: extension = php_soap.dll extension = php_curl.dll extension = php_openssl.
1180 0
PHP使用SOAP调用.net的WebService数据
需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路有点不一样,需要用到SOAP模块,处理方法也很简单,就是有一些需要注意的事情。
1188 0
PHP调用WebService接口
WebService是一个提供外部使用的一个服务,使用PHP去调用它其实是很简单的,写一个demo如下: 1、首先你的PHP要支持SOAP 检测PHP是否支持SOAP打印phpinfo(),...
1453 0
WebService /php soap 相关调用 收藏
整理了一下浏览器收藏夹里的东西,发现好多连接都死掉了 C#动态调用Web服务的3种方法 http://developer.51cto.com/art/200908/144594.htm C#调用WebService制作天气预报 http://www.2cto.com/kf/201007/52347.html JS调用WebService示例 http://www.cnblog
1203 0
+关注
技术小胖子
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
PHP 2017.北京 全球开发者大会——高可用的PHP
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多