gpt4 book ai didi

PHP 且无 Soap 操作 header

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:48:31 25 4
gpt4 key购买 nike

我正在尝试调用公司内部的远程网络服务。由于专有原因,我无法提供 Web 服务的 URL。 Web 服务有一个名为 getItemField 的函数。这是我尝试运行 PHP 的小型测试服务,服务描述如下:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://www.oracle.com/ws/MyFirstWebService" xmlns:intf="http://www.oracle.com/ws/MyFirstWebService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://www.w3.org/1999/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.oracle.com/ws/MyFirstWebService">
<!--
WSDL created by Apache Axis version: 1.2alpha Built on Oct 23, 2007 (12:09:54 IST)
-->
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.oracle.com/ws/MyFirstWebService">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complexType name="ArrayOf_xsd_string">
<complexContent>
<restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]"/>
</restriction>
</complexContent>
</complexType>
</schema>
</wsdl:types>
<message name="getItemFieldRequest">
<part name="args" type="impl:ArrayOf_xsd_string"/>
</message>
<message name="getItemFieldResponse">
<part name="getItemFieldReturn" type="soapenc:string"/>
</message>
<portType name="MyFirstWebService">
<operation name="getItemField" parameterOrder="args">
<input message="impl:getItemFieldRequest" name="getItemFieldRequest"/>
<output message="impl:getItemFieldResponse" name="getItemFieldResponse"/>
</operation>
</portType>
<binding name="MyFirstWebServiceSoapBinding" type="impl:MyFirstWebService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getItemField">
<wsdlsoap:operation soapAction=""/>
<input name="getItemFieldRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://first" use="encoded"/>
</input>
<output name="getItemFieldResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://www.oracle.com/ws/MyFirstWebService" use="encoded"/>
</output>
</operation>
</binding>
<service name="MyFirstWebServiceService">
<port binding="impl:MyFirstWebServiceSoapBinding" name="MyFirstWebService">
<wsdlsoap:address location="http://myWebsite.com/services/MyFirstWebService"/>
</port>
</service>
</definitions>

我可以很好地连接到该服务并打印出单个函数的名称和类型,但是当我尝试调用一个函数时,我得到了'no SOAPAction header!' 错误。我调用服务函数的PHP代码如下:

$options = array(
// Dev
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,

// Credentials
'login' => 'user',
'password' => 'pass'
);

// Connected successfully
$client = new SoapClient( "http://myWebsite.com/services/MyFirstWebService?wsdl", $options );

//Params
$params = array( '123456' );

//Options
$options = array( 'soapaction' => '""' );

//Call function (Both methods below throw the same 'no SOAPAction header!' error)
//$result = $client->getItemField( new SoapParam($params, "getItemFieldRequest") );
$result = $client->__soapCall( "getItemField", array('123456'), $options );

根据服务的描述,它似乎没有定义 soapaction。我在 soapAction 中看到的那一行是 <wsdlsoap:operation soapAction=""/> 。我试图指定一个空白的 soapAction,因为没有定义,但这似乎不起作用。 Web 服务定义本身是否不完整?还是我在客户端应用程序调用中缺少某些参数?提前致谢。

更新:

尝试将方法名称指定为 soap 操作,但 htat 不起作用。

    //Params
$params = array( '123456' );

//Options
$options = array( 'soapaction' => 'getItemField' );

//Call function (STILL THROWS 'no SOAPAction header!')
$result = $client->__soapCall( "getItemField", $params, $options );

如果在 wsdl 中没有指定 soapAction,我应该怎么办,即如果它设置为“”。

最佳答案

我遇到了类似的问题,但我使用的是 cURL 而不是 SoapClient ,尽管解决方案可能相同。

当向终端服务发送请求时,您必须发送一个 header 指定Content-Type。在此 header 中,您还可以指定 action 或“soap action”。

$headers = array();
$headers[] = 'Content-Type: application/soap+xml; charset=utf-8; action="[soap action goes here]"';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

在我的例子中,wsdl 列出了以下内容,看起来与您的相同:

<wsdl:operation name="getProducts">
<soap:operation soapAction=""/>
<!-- trimmed -->
</wsdl:operation>

首先,我看到 soapAction="",所以我在发送的 header 中设置了 action=""。这失败了。进入 header 的正确数据是 operation,使其成为 action="getProducts"

根据 SoapClient 的手册和对 php using SOAP with a different SoapAction 的回答,您的请求应该适用于:

$client = new SoapClient( "http://myWebsite.com/services/MyFirstWebService?wsdl", $options );
$result = $client->__soapCall( "getItemField", array('123456') );

您是否曾忽略发送到 __soapCall()$options 以简单地不指定操作(而不是传递空白操作)?

关于PHP 且无 Soap 操作 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8220585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com