PHP SOAP:如何使用 SOAP 从 PHP 返回对象? [英] PHP SOAP : How can I return objects from PHP using SOAP?

查看:25
本文介绍了PHP SOAP:如何使用 SOAP 从 PHP 返回对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 SOAP 向 PHP 发送/返回对象或数组.有什么好的链接吗?

I need to send/return objects or array to/from PHP using SOAP. Any good links?

推荐答案

我正在使用 Zend_Soap_Server и Zend_Soap_Client.我发送/接收复杂结构的数组.

I am using Zend_Soap_Server и Zend_Soap_Client. I send/receive array of difficult structure.

首先创建具有您想要接收结构的类.

At first create class with structure you want to receive.

<?php

/**
 * Information about people
 */
class PeopleInformation
{
    /**
     * Name of ...
     *
     * @var string
     */
    public $name;

    /**
     * Age of
     * @var int
     */
    public $age;

    /**
     * Array of family
     *
     * @var FamilyInformation[]
     */
    public $family;
}

/**
 * Information about his family
 */
class FamilyInformation
{
    /**
     * Mother/sister/bro etc
     *
     * @var string
     */
    public $relation;

    /**
     * Name
     * @var string
     */
    public $name;
}

?>

然后创建服务来接收这些数据:

Then create service to receive this data:

<?php

/**
 * Service to receive SOAP data
 */
class SoapService
{
    /**
     *
     * @param PeopleInformation $data
     * @return string
     */
    public function getUserData($data)
    {
        //here $data is object of PeopleInformation class


        return "OK";
    }
}
?>

现在通过 url 在控制器中创建 Zend_Soap_Server 实例 http://ourhost/soap/:

Now create Zend_Soap_Server instance in controller by url http://ourhost/soap/:

<?php

//disable wsdl caching
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache', 0);

$wsdl = $_GET['wsdl'];

//this generate wsdl from our class SoapService
if (!is_null($wsdl))
{
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence');
    $autodiscover->setClass('SoapService');
    $autodiscover->handle();
}
//handle all soap request
else
{
    $wsdlPath = 'http://ourhost/soap/?wsdl';

    $soap = new Zend_Soap_Server($wsdlPath, array(
        'cache_wsdl' => false
    ));
    $soap->registerFaultException('Zend_Soap_Server_Exception');
    $soap->setClass('SoapService');
    $soap->handle();
}

?>

现在你得到了 wsdl (http://ourhost/soap/?wsdl),你可以在 SoapService::getUserData 中构建和处理请求.该方法的输入参数是PeopleInformation

And now you get wsdl (http://ourhost/soap/?wsdl) with you structure and handle request in SoapService::getUserData. Input parametr in this method is object of PeopleInformation class

这篇关于PHP SOAP:如何使用 SOAP 从 PHP 返回对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆