如何在Yii中创建Web服务SOAP 1.2

时间:2012-04-20 13:31:40

标签: php web-services soap yii

问题:

我无法弄清楚Yii希望我如何改变肥皂版本。我正在关注这个tutorial。 Web服务可以工作,但它不是SOAP 1.2

班级:

<?php
class ABFolioController extends CController
{
    public function actions()
    {
        return array(
            'folio'=>array(
                'class'=>'CWebServiceAction',
                'serviceOptions'=>array('soapVersion'=>'1.2')
            ),
        );

    }

    /**
     * @param  string folioId
     * @param  string cNumber
     * @param  string oDRS
     * @param  string dlr
     * @param  string feeAmount
     * @param  string transactionStatus
     * @param  string captureId
     * @param  datetime captureTimestamp
     * @param  string prefix
     * @param  string oDEFRS
     * @return string the statement
     * @soap
     */
    public function sendFolio($folioId, $cNumber, $oDRS, $dlr,
            $feeAmount, $transactionStatus, $captureId, $captureTimestamp, 
            $prefix, $oDEFRS)
    {
    //

      $model = new Dlfolio();
      $model->folioId = $folioId;
      $model->cNumber = $cNumber;
      $model->oDRS = $oDRS;
      $model->dlr = $dlr;
      $model->feeAmount = $feeAmount;
      $model->transactionStatus = $transactionStatus;
      $model->captureId = $captureId;
      $model->captureTimestamp = $captureTimestamp;
      $model->prefix = $prefix;
      $model->oDEFRS = $oDEFRS;
      $yesno = $model->save();

      if ($yesno=TRUE)
      {
          //on success
          return 'Record Saved';
      }
      else
      {
          //on failure
          return $yesno;
      }

    }   
}

1 个答案:

答案 0 :(得分:4)

当我将客户端设置为v1.2时,我总是从服务器收到v1.2响应,当我将客户端设置为v1.1时,我总是从服务器收到v1.1响应,也许它会自动检测客户端版本并用它覆盖服务器版本?

$client=new SoapClient('http://hys.local/ABFolio/folio',array('soap_version'=>SOAP_1_2,'trace'=>true));
var_dump($client);
echo $client->sendFolio();
echo $client->__getLastRequest();
echo $client->__getLastResponse();

响应是1.2

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"

和1.1客户端的默认值

$client=new SoapClient('http://hys.local/ABFolio/folio',array('soap_version'=>SOAP_1_1,'trace'=>true));

响应是1.1

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

EDIT:

让我们看看yii framework\web\services\CWebService.php

/**
 * @return array options for creating SoapServer instance
 * @see http://www.php.net/manual/en/function.soap-soapserver-construct.php
 */
protected function getOptions()
{
    $options=array();
    if($this->soapVersion==='1.1')
        $options['soap_version']=SOAP_1_1;
    else if($this->soapVersion==='1.2')
        $options['soap_version']=SOAP_1_2;
    if($this->actor!==null)
        $options['actor']=$this->actor;
    $options['encoding']=$this->encoding;
    foreach($this->classMap as $type=>$className)
    {
        $className=Yii::import($className,true);
        if(is_int($type))
            $type=$className;
        $options['classmap'][$type]=$className;
    }
    return $options;
}

如果我检查此代码

,我的代码中没有任何错误

EDIT:

好吧,这比这个怎么样? framework\web\services\CWsdlGenerator.php

/*
 * @param DOMDocument $dom Represents an entire HTML or XML document; serves as the root of the document tree
 */
private function addBindings($dom)
{
    $binding=$dom->createElement('wsdl:binding');
    $binding->setAttribute('name',$this->serviceName.'Binding');
    $binding->setAttribute('type','tns:'.$this->serviceName.'PortType');

    $soapBinding=$dom->createElement('soap:binding');
    $soapBinding->setAttribute('style','rpc');
    $soapBinding->setAttribute('transport','http://schemas.xmlsoap.org/soap/http');
    $binding->appendChild($soapBinding);

    $dom->documentElement->appendChild($binding);

    foreach($this->_operations as $name=>$doc)
        $binding->appendChild($this->createOperationElement($dom,$name));
}

因为我可以看到预定义的传输(您可以检查并将其替换为12)

我的wsdl在添加12后变为此

<wsdl:binding name="ABFolioControllerBinding" type="tns:ABFolioControllerPortType">    
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap12/http"/>
</wsdl:binding>

也许是yii中的错误 - 继续然后再报告

但是从http://msdn.microsoft.com/en-us/library/ms995800.aspx起,我检查的不是传输而是命名空间

SOAP versioning is based on XML namespaces. SOAP 1.1 is identified by the http://schemas.xmlsoap.org/soap/envelope/ namespace while SOAP 1.2 is identified by the http://www.w3.org/2002/12/soap-envelope namespace (although this will change when it becomes a Recommendation). 

这就是我认为一切正确的原因

EDIT:

这里有瑕疵,你需要

xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
除了

之外,

xml中的<definitions内部

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 

而不是:

<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

你必须放置

<soap12:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

现在soapUI检测到我的wsdl为soap 1.2

<?xml version="1.0" encoding="UTF-8"?>
<definitions 
xmlns="http://schemas.xmlsoap.org/wsdl/" 
xmlns:tns="urn:ABFolioControllerwsdl" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" 
name="ABFolioController" 
targetNamespace="urn:ABFolioControllerwsdl">
    <wsdl:portType name="ABFolioControllerPortType"/>
    <wsdl:binding name="ABFolioControllerBinding" type="tns:ABFolioControllerPortType">
        <soap12:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    </wsdl:binding>
    <wsdl:service name="ABFolioControllerService">
        <wsdl:port name="ABFolioControllerPort" binding="tns:ABFolioControllerBinding">
            <soap:address location="http://hys.local/uk/aBFolio/folio?ws=1"/>
        </wsdl:port>
    </wsdl:service>
</definitions>

您可以在addBindingsbuildDOM函数中的yii中的相同文件中进行所有替换

我认为它也更难,即如果你想同时支持soap和soap12绑定,那么至少它已经被认为是soap12

EDIT:

yii hardcoded soap1.1如果你不提供自己的wsdl(你可以通过$ wsdlUrl提供它,就像在CWebService的run方法中一样)。它似乎是合法的 - 因为php soap服务器中的默认肥皂版本也是1.1,如果你将你的版本改为1.2你必须提供你自己的wsdl for 1.2