You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP调用SOAP服务报VersionMismatch错误,SOAP UI执行正常

解决PHP调用SOAP Web服务时的[VersionMismatch] Wrong Version错误

我在PHP中调用SOAP Web服务时碰到了一个棘手的问题:触发了SoapFault异常,错误信息是**[VersionMismatch] Wrong Version**。但奇怪的是,我把$client->__getLastRequest()输出的请求内容拿到SOAP UI里执行,服务端却能正常返回结果。

我的环境信息

WSDL内容(已替换服务器名为localhost)

<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:PDF_API" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:PDF_API" >
<types>
<xsd:schema targetNamespace="urn:PDF_API"></xsd:schema>
</types>
<message name="html_to_pdf_request">
<part name="html" type="xsd:string" />
</message>
<message name="html_to_pdf_response">
<part name="pdf" type="xsd:string" />
</message>
<portType name="PDF_API_PortType">
<operation name="html_to_pdf">
<documentation>Convertit le HTML en format PDF</documentation>
<input message="tns:html_to_pdf_request"/>
<output message="tns:html_to_pdf_response"/>
</operation>
</portType>
<binding name="PDF_API_Binding" type="tns:PDF_API_PortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="html_to_pdf">
<soap:operation soapAction="urn:html_to_pdf2/html_to_pdf" style="rpc"/>
<input><soap:body use="encoded" namespace="urn:html_to_pdf2" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></input>
<output><soap:body use="encoded" namespace="urn:html_to_pdf2" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></output>
</operation>
</binding>
<service name="PDF_API">
<port name="PDF_API_Port" binding="tns:PDF_API_Binding">
<soap:address location="http://localhost/PDF_API/PDF_API.php"/>
</port>
</service>
</definitions>

PHP调用代码

try{
$client = new SoapClient( pdfApiWSDL, array( 'soap_version' => SOAP_1_1, 'cache_wsdl' => WSDL_CACHE_NONE, 'trace'=>1) );
$client->__setLocation(pdfApiWSDL);
$res = $client->html_to_pdf('test');
} catch (Exception $e) { }

生成的请求信封

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:html_to_pdf2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
<env:Body>
<ns1:html_to_pdf env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<html xsi:type="xsd:string">test </html>
</ns1:html_to_pdf>
</env:Body>
</env:Envelope>

排查与解决过程

我查了不少相关资料,尝试调整SOAP版本、检查请求命名空间等,但都没解决问题。最后才发现,问题出在WSDL的请求链接上:

原来我用的是 http://myserver/PDF_API/PDF_API.php.wsdl,而正确的链接应该是 http://myserver/PDF_API/PDF_API.php?wsdl。修改这个链接后,问题立即得到解决,SOAP服务调用正常了。

内容的提问来源于stack exchange,提问作者Rick31

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:47:48