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,如需转载请自行联系原作者






目录
打赏
0
0
0
0
347
分享
相关文章
PHP命名空间深度解析:避免命名冲突与提升代码组织####
本文深入探讨了PHP中命名空间的概念、用途及最佳实践,揭示其在解决全局命名冲突、提高代码可维护性方面的重要性。通过生动实例和详尽分析,本文将帮助开发者有效利用命名空间来优化大型项目结构,确保代码的清晰与高效。 ####
80 20
|
4月前
|
探索PHP中的异常处理:提升代码的健壮性
在PHP开发中,优雅地处理错误和异常是确保应用稳定性和用户体验的关键。本文将通过深入浅出的方式,介绍如何在PHP中实现有效的异常处理机制,包括异常的基本概念、如何抛出和捕获异常,以及最佳实践。准备好让你的代码变得更加健壮和可靠吧!
40 2
PHP 互斥锁:如何确保代码的线程安全?
在多线程和高并发环境中,确保代码段互斥执行至关重要。本文介绍了 PHP 互斥锁库 `wise-locksmith`,它提供多种锁机制(如文件锁、分布式锁等),有效解决线程安全问题,特别适用于电商平台库存管理等场景。通过 Composer 安装后,开发者可以利用该库确保在高并发下数据的一致性和安全性。
65 6
PHP中的异常处理:提升代码的健壮性
【10月更文挑战第40天】在PHP编程中,异常处理是确保应用稳定性的关键。本文将引导你理解异常处理的重要性,掌握如何在PHP中捕获和处理异常,以及如何通过自定义异常类来增强代码的错误管理能力。我们将一起探索如何利用PHP的异常处理机制,打造一个更加健壮和可靠的应用程序。
PHP中的类型提示与严格模式:提高代码可维护性
随着PHP语言的发展,开发者对代码的可读性、可维护性和可靠性有了更高的要求。PHP中的类型提示(Type Hinting)和严格模式(Strict Mode)为开发者提供了更强的类型检查机制,有助于提升代码质量和减少潜在的错误,尤其是在大型项目中。
PHP与SOAP Web服务开发:基础与进阶教程
本文介绍了PHP与SOAP Web服务的基础和进阶知识,涵盖SOAP的基本概念、PHP中的SoapServer和SoapClient类的使用方法,以及服务端和客户端的开发示例。此外,还探讨了安全性、性能优化等高级主题,帮助开发者掌握更高效的Web服务开发技巧。
PHP中的设计模式:提高代码的可维护性和扩展性
【10月更文挑战第13天】 本文将探讨PHP中常见的设计模式及其在实际项目中的应用。通过对比传统编程方式,我们将展示设计模式如何有效地提高代码的可维护性和扩展性。无论是单例模式确保类的单一实例,还是观察者模式实现对象间的松耦合,每一种设计模式都为开发者提供了解决特定问题的最佳实践。阅读本文后,读者将能更好地理解和应用这些设计模式,从而提升PHP编程的效率和质量。
PHP中的异常处理:提升代码的健壮性
【10月更文挑战第8天】在编程世界中,错误和异常是不可避免的。它们如同路上的绊脚石,让我们的程序跌倒。但如果我们学会了如何处理这些异常,就能像学会走路的孩子一样,即使跌倒也能爬起来继续前进。本文将引导你如何在PHP中捕获并处理异常,让你的代码更加健壮,能够优雅地面对各种挑战。
PHP中的异常处理:提升代码的健壮性
【10月更文挑战第8天】在编程的世界中,错误和异常是不可避免的。它们就像路上的坑洼,可能会让我们的程序“跌倒”。但是,如果我们能够正确地处理这些异常,就可以让我们的程序更加稳健,就像我们学会了如何在坑洼的路上稳稳地行走一样。本文将介绍PHP中的异常处理机制,以及如何使用它来提升我们的代码质量。
PHP中的设计模式:单例模式的深入探索与实践在PHP开发领域,设计模式是解决常见问题的高效方案集合。它们不是具体的代码,而是一种编码和设计经验的总结。单例模式作为设计模式中的一种,确保了一个类仅有一个实例,并提供一个全局访问点。本文将深入探讨单例模式的基本概念、实现方式及其在PHP中的应用。
单例模式在PHP中的应用广泛,尤其在处理数据库连接、日志记录等场景时,能显著提高资源利用率和执行效率。本文从单例模式的定义出发,详细解释了其在PHP中的不同实现方法,并探讨了使用单例模式的优势与注意事项。通过对示例代码的分析,读者将能够理解如何在PHP项目中有效应用单例模式。

热门文章

最新文章