PHP中的SOAP错误:OperationFormatter遇到无效的Message主体

时间:2018-05-14 11:28:20

标签: php soap wsdl soapui soap-client

原始问题:

我试图从SOAP API wsdl链接获取数据。我的代码如下。但是我得到了这个错误。任何人都可以帮忙吗?

  

错误消息:OperationFormatter遇到无效的Message正文。   预计会找到节点类型'元素'名字' GetLocalRates'和   命名空间' http://tempuri.org/'。找到节点类型'元素'有名字   ' soapenv:信封'和命名空间   ' http://www.w3.org/2003/05/soap-envelope'

<?php
$api_link = 'https://www.my-api-link.com/RateAPI.svc/SSL?wsdl';

//setting xml request to api
$request = '<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:ezr="http://schemas.datacontract.org/2004/07/EzremitAPI.Entities">
              <soapenv:Body>
                 <tem:GetLocalRates>
                     <tem:credentials>
                         <ezr:AgentCode>####</ezr:AgentCode>
                         <ezr:HashedPassword>####</ezr:HashedPassword>
                         <ezr:Username>####</ezr:Username>
                     </tem:credentials>
                     <tem:payincurcode>####</tem:payincurcode>
                     <tem:transferType>####</tem:transferType>
                 </tem:GetLocalRates>
              </soapenv:Body>
            </soapenv:Envelope>';

try {
$client = new SoapClient($api_link, array('cache_wsdl' => WSDL_CACHE_NONE, 'soap_version' => SOAP_1_2, 'reliable' => 1.2 , 'useWSA' => TRUE ) );
$soapaction = "http://tempuri.org/IRateAPI/GetLocalRates";
$client->soap_defencoding = 'UTF-8';
// Apply WSA headers
$headers = array();
$headers[] = new SoapHeader('http://www.w3.org/2005/08/addressing', 'To', 'https://www.my-api-link.com/RateAPI.svc/SSL?wsdl', true);
$headers[] = new SoapHeader('http://www.w3.org/2005/08/addressing', 'Action', 'http://tempuri.org/IRateAPI/GetLocalRates', true);
$client->__setSoapHeaders($headers);

$response = $client->GetLocalRates(new SoapVar($request, XSD_ANYXML));
print_r($response);

}
  catch(Exception $e) {
    echo $e->getMessage();
}
?>

编辑1(根据第1条评论修改的代码)

结果:

http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/faults:Receivera:InternalServiceFaultOperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetLocalRates' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'soapenv:Envelope' and namespace 'http://www.w3.org/2003/05/soap-envelope'OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetLocalRates' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'soapenv:Envelope' and namespace 'http://www.w3.org/2003/05/soap-envelope' at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
 at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
 at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
 at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
 at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)System.Runtime.Serialization.SerializationException

编辑2:

$ client-&gt; __ getTypes()结果:

array(8) {
  [0]=>
  string(84) "struct EzCredential {
 string AgentCode;
 string HashedPassword;
 string Username;
}"
  [1]=>
  string(58) "struct ArrayOfCurrencyRate {
 CurrencyRate CurrencyRate;
}"
  [2]=>
  string(187) "struct CurrencyRate {
 decimal AgentMargin;
 string CurrencyCode;
 string CurrencyDescription;
 decimal FromAmount;
 decimal Rate;
 string RateType;
 decimal ToAmount;
 string Trantype;
}"
  [3]=>
  string(95) "struct GetLocalRates {
 EzCredential credentials;
 string payincurcode;
 string transferType;
}"
  [4]=>
  string(74) "struct GetLocalRatesResponse {
 ArrayOfCurrencyRate GetLocalRatesResult;
}"
  [5]=>
  string(8) "int char"
  [6]=>
  string(17) "duration duration"
  [7]=>
  string(11) "string guid"
}

$ client-&gt; __ getFunctions()结果:

array(1) {
  [0]=>
  string(62) "GetLocalRatesResponse GetLocalRates(GetLocalRates $parameters)"
}

修正:使用下面而不是XML信封。非常感谢,@ Marcel。你是一个伟大的救世主。

$requestParams = array( 'credentials' => array('AgentCode' => $acode,
                                               'HashedPassword' => $hpass,
                                               'Username' => $uname),
                  'payincurcode' => $ccode,
                  'transferType' => $ttype
              );

$response = $client->GetLocalRates( $requestParams );

1 个答案:

答案 0 :(得分:0)

提前感谢您使用缺失的数据更新了问题。此示例是一个本机php示例,演示如何以面向对象的方式使用soap函数和类型。

<强> 1。类型和类

正如您在types数组中看到的,有几种类型声明为 struct 。如果你愿意这样说,结构可以称为PHP类。所以让我们从每个结构中创建一个php类。

class EzCredentialStruct
{
    public $AgentCode;

    public $HashedPassword;

    public $Username;

    public function getAgentCode() : string
    {
        return $this->AgentCode;
    }

    public function setAgentCode(string $AgentCode) : EzCredentialStruct
    {
        $this->AgentCode = $AgentCode;
        return $this;
    }

    public function getHashedPassword() : string
    {
        return $this->HashedPassword;
    }

    public function setHashedPassword(string $HashedPassword) : EzCredentialStruct
    {
        $this->HashedPassword = $HashedPassword;
        return $this;
    }

    public function getUsername() : string
    {
        return $this->Username;
    }

    public function setUsername(string $Username) : EzCredentialStruct
    {
        $this->Username = $Username;
        return $this;
    }
}

class GetLocalRatesStruct
{
    public $credentials;

    public $payincurcode;

    public $transferType;

    public function getCredentials() : EzCredentialStruct
    {
        return $this->credentials;
    }

    public function setCredentials(EzCredentialStruct $credentials) : GetLocalRatesStruct
    {
        $this->credentials = $credentials;
        return $this;
    }

    public function getPayincurcode() : string
    {
        return $this->payincurcode;
    }

    public function setPayincurcode(string $payincurcode) : GetLocalRatesStruct
    {
        $this->payincurcode = $payincurcode;
        return $this;
    }

    public function getTransferType() : string
    {
        return $this->transferType;
    }

    public function setTransferType(string $transferType) : GetLocalRatesStruct
    {
        $this->transferType = $transferType;
        return $this;
    }
}

这两个类是types数组中所有结构的示例。因此,将所有结构写下来作为类。您稍后会注意到这个好处。

<强> 2。 Soap Client和classmap选项

现在,当我们将webservice的已使用类型声明为类时,我们可以启动soap客户端。使用正确的选项启动soap客户端非常重要。始终将客户端包装在try / catch块中。

try {
    $options = [
        'cache_wsdl' => WSDL_CACHE_NONE, 
        'soap_version' => SOAP_1_2,
        'trace' => true,
        'classmap' => [
            'EzCredential' => 'EzCredentialStruct',
            'GetLocalRates' => 'GetLocalRatesStruct',
        ],
    ];

    $wsdl = 'path/to/the/webservice/endpoint?wsdl';

    $client = new \SoapClient(
        $wsdl,
        $options,
    );
} catch (\SoapFault $e) {
    // error handling
}

如您所见,options数组中有一个classmap键。 classmap将类型路由到特定的php类。在这个例子中,我们只使用前面定义的两个示例类型类。 soap客户端现在可以自动创建webservice所需的xml字符串。

第3。全部放在一起

现在,正如我们所需要的所有肥皂请求一样,我们的代码应该如下所示。

$credentials = (new EzCredentialStruct())
    ->setAgentCode($agentCode)
    ->setHashedPassword($hashedPassword)
    ->setUsername($username);

$request = (new GetLocalRatesStruct())
    ->setCredentials($credentials)
    ->setPayincurcode($code)
    ->setTransferType($transferType);

$result = $client->GetLocalRates($request);

<强>结论

乍一看,这种方法可能看起来更像是工作。但是,将这些类型与webservice声明的每个结构的函数和编程类分开是有意义的。 webservice函数的调用将更容易,因为我们只传递一个对象作为参数。没有更奇怪的数组结构。一切都取而代之,可以重复使用。

PS:此代码未经过测试。不建议在生产中使用它。此代码仅作为示例。它可能包含错误。