大佬教程收集整理的这篇文章主要介绍了php – 使用NuSOAP生成WSDL – 返回具有各种类型的结构(int,string,结构数组),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我成功地生成了WSDL代码,它只返回一个结构数组(关联数组),但我宁愿返回一个包含整数变量,字符串变量和结构数组的对象(struct).
所以,这是用于返回结构数组的代码:
<?PHP function getstuffs( $user='',$pass='' ) { // here we can check user and pass and do whatever (if it isn't alright,we can throw exception or return NULL or sg. similar) // ....... $stuff_array = array(); $stuff_arraY[] = array( 'id'=>122,'name'=>'One stuff'); $stuff_arraY[] = array( 'id'=>213,'name'=>'Another stuff'); $stuff_arraY[] = array( 'id'=>435,'name'=>'whatever stuff'); $stuff_arraY[] = array( 'id'=>65,'name'=>'Cool stuff'); $stuff_arraY[] = array( 'id'=>92,'name'=>'Wow,what a stuff'); return $stuff_array; } require_once 'nusoap/lib/nusoap.PHP'; $server = new soap_server; // $myNamespace = $_SERVER['SCRIPT_URI']; $myNamespace = 'http://'.$_SERVER['http_HOST'].$_SERVER['SCRIPt_name']; $server->configureWSDL('Mystuffservice','urn:' . $myNamespacE); // $server->wsdl->scheR_522_11845@aTargetNamespace = 'http://soapinterop.org/xsd/'; $server->wsdl->addComplexType( // name 'stuffs',// typeClass (complexType|simpleType|attributE) 'complexType',// PHPType: currently supported are array and struct (PHP assoc array) 'struct',// compositor (all|sequence|choicE) 'all',// reStrictionBase namespace:name (http://scheR_522_11845@as.xmlsoap.org/soap/encoding/:Array) '',// elements = array ( name = array(name=>'',type=>'') ) array( 'id' => array( 'name' => 'id','type' => 'xsd:int' ),'name' => array( 'name' => 'name','type' => 'xsd:string' ) ) ); $server->wsdl->addComplexType( // name 'stuffsArray',// PHPType: currently supported are array and struct (PHP assoc array) 'array',// compositor (all|sequence|choicE) '',// reStrictionBase namespace:name (http://scheR_522_11845@as.xmlsoap.org/soap/encoding/:Array) 'SOAP-ENC:Array',type=>'') ) array(),// attrs array( array( 'ref' => 'SOAP-ENC:arrayType','wsdl:arrayType' => 'tns:stuffs[]' ) ),// arrayType: namespace:name (http://www.w3.org/2001/XMLscheR_522_11845@a:string) 'tns:stuffs' ); $server->register( // String $name the name of the PHP function,class.method or class..method 'getstuffs',// array $in assoc array of input values: key = param name,value = param type array( 'user' => 'xsd:string','pass' => 'xsd:string' ),// array $out assoc array of output values: key = param name,value = param type array( 'return' => 'tns:stuffsArray' ),// mixed $namespace the element namespace for the method or false 'urn:' . $myNamespace,// mixed $soapaction the soapaction for the method or false 'urn:' . $myNamespace . "#getstuffs",// mixed $style optional (rpc|document) or false Note: when 'document' is specified,parameter and return wrappers are created for you automatically 'rpc',// mixed $use optional (encoded|literal) or false 'encoded',// String $documentation optional Description to include in WSDL 'Fetch array of stuffs ("id","name").' // documentation ); #$server->wsdl->scheR_522_11845@aTargetNamespace = $myNamespace; $server->service(isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''); exit(); ?>
在C#控制台应用程序中,在添加名为“stuffservice”的Web引用并将“?wsdl”附加到可以找到此PHP文件的相应URL之后,此代码可以正常工作,我可以完美地查询stuff_array值,如下所示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WebserviCETest { class Program { static void Main(String[] args) { stuffservice.Mystuffservice myservice = new stuffservice.Mystuffservice(); stuffservice.stuffs[] stuffs = myservice.getstuffs("someone","1234"); foreach (var stuff in stuffs) { Console.WriteLine(stuff.id+".: "+stuff.Name); } Console.WriteLine(); Console.WriteLine("Press a key..."); Console.ReadKey(); } } }
这很酷,但我想开发这个代码来回馈这样的对象:
class ResponSEObject { public $responseCode = 0; public $responsemessage = ''; public $stuffArray = NULL; } $responSEObject = NULL; function getstuffs( $user='',$pass='' ) { global $responSEObject; $responSEObject = new ResponSEObject(); // check stuffs in a simple way Now if($user != 'someone' && $pass != '1234'){ $responSEObject->responseCode = 2; $responSEObject->responsemessage = 'Authentication Failed'; return $responSEObject; } $responSEObject->stuffArray = array(); $responSEObject->stuffArraY[] = array( 'id'=>122,'name'=>'One stuff'); $responSEObject->stuffArraY[] = array( 'id'=>213,'name'=>'Another stuff'); $responSEObject->stuffArraY[] = array( 'id'=>435,'name'=>'whatever stuff'); $responSEObject->stuffArraY[] = array( 'id'=>65,'name'=>'Cool stuff'); $responSEObject->stuffArraY[] = array( 'id'=>92,what a stuff'); $responSEObject->responseCode = 1; $responSEObject->responsemessage = 'successful!'; return $responSEObject; }
适合的NuSOAP代码是什么?
谢谢!!
以上是大佬教程为你收集整理的php – 使用NuSOAP生成WSDL – 返回具有各种类型的结构(int,string,结构数组)全部内容,希望文章能够帮你解决php – 使用NuSOAP生成WSDL – 返回具有各种类型的结构(int,string,结构数组)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。