php – 使用NuSOAP生成WSDL – 返回具有各种类型的结构(int,string,结构数组)

前端之家收集整理的这篇文章主要介绍了php – 使用NuSOAP生成WSDL – 返回具有各种类型的结构(int,string,结构数组)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过生成带有 NuSOAP的WSDL来通过SOAP查询一些内容.
我知道有很多与该主题相关的问题,但我没有成功地使代码适应我的特定问题.

我成功地生成了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->schemaTargetNamespace = '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://schemas.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://schemas.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/XMLSchema: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->schemaTargetNamespace = $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代码是什么?
谢谢!! 原文链接:https://www.f2er.com/php/137844.html

猜你在找的PHP相关文章