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写个原生增删改查案例出来(提供全部代码+sql)
拿php写个原生增删改查案例出来(提供全部代码+sql)
拿php写个原生增删改查案例出来(提供全部代码+sql)
|
1月前
|
PHP 开发者 UED
PHP 中的异常处理:提高代码健壮性的关键
【2月更文挑战第28天】在 PHP 开发中,异常处理是确保应用程序稳定性和可靠性的重要环节。本文将深入探讨 PHP 异常的概念、类型及其处理机制,并通过实例演示如何有效地捕获和处理异常,以增强代码的健壮性和用户体验。
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
php案例:用代码的方式创建目录+文件+写入数据(都由你定)
|
3月前
|
前端开发 JavaScript PHP
|
3月前
|
前端开发 JavaScript 关系型数据库
PHP代码合集21个邮箱2个问答23个ajax特效
PHP代码合集21个邮箱2个问答23个ajax特效
18 0
|
8月前
|
前端开发 JavaScript 机器人
用PHP实现了一个极验验证功能,如何做?具体代码如何写?
极验验证是一种防机器人的验证机制,可以通过图像识别等方式来判断用户是否为真实用户。
91 1
|
9月前
|
JSON 前端开发 JavaScript
Echarts实战案例代码(15):月收入年龄分段等MYSQL分类统计PHP后台数据管理接口API数据的解决方案
Echarts实战案例代码(15):月收入年龄分段等MYSQL分类统计PHP后台数据管理接口API数据的解决方案
131 0