繁体   English   中英

如何使用带有客户端证书的 SoapClient 连接到 SOAP Web 服务?

[英]How to connect to a SOAP webservice using SoapClient with a client certificate?

我组装了一个本地 Soap 服务器用于测试目的。 我创建了一个客户端证书来连接它。

从 SoapUI 调用服务时,我可以正常连接,但使用SoapClient时,我似乎无法正常工作。

我尝试直接打开 WSDL(没有 SoapClient)以查看连接是否有效并且确实有效,但是当使用完全相同的 stream 上下文作为构造函数的选项时,它会产生 SoapFault 异常:“无法连接到主机”。

这是我的代码,任何人都可以看到它有什么问题吗? 谢谢

<?php

$context = stream_context_create(
    [
        'ssl' => [
            'local_cert' => __DIR__ . '/client.crt',
            'local_pk' => __DIR__ . '/client.key',
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => false,
        ]
    ]
);

$url = 'https://localhost:4430?WSDL';

echo "From file_get_contents: ".(file_get_contents($url, false, $context)).PHP_EOL;

echo "== Making the call ==".PHP_EOL;

try {
    $client = new SoapClient($url,
        [
            'stream_context' => $context,
        ]);

    echo "From ws call: ".$argv[1] . ' + ' . $argv[2] . ' = ' . $client->Add([
            'intA' => $argv[1],
            'intB' => $argv[2],
        ])->AddResult;
} catch (SoapFault $soapFault) {
    echo $soapFault->getMessage();
}

echo PHP_EOL;

这是我得到的 output:

From file_get_contents: <?xml version="1.0" encoding="UTF-8"?>
<definitions name="Calculator"
             xmlns = "http://schemas.xmlsoap.org/wsdl/"
             targetNamespace="urn:Calculator"
             xmlns:tns="urn:Calculator"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
    <message name="addRequest">
        <part name="a" type="xsd:int"/>
        <part name="b" type="xsd:int"/>
    </message>
    <message name="addResponse">
        <part name="result" type="xsd:int"/>
    </message>
    <portType name="AddPort">
        <operation name="add">
            <input message="tns:addRequest"/>
            <output message="tns:addResponse"/>
        </operation>
    </portType>
    <binding name="AddBinding" type="tns:AddPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="add">
            <soap:operation soapAction="urn:CalculatorAction"/>
            <input>
                <soap:body use="encoded" namespace="urn:Calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </input>
            <output>
                <soap:body use="encoded" namespace="urn:Calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </output>
        </operation>
    </binding>
    <service name="WSDLService">
        <documentation>Returns a greeting string.</documentation>
        <port name="AddPort" binding="tns:AddBinding">
            <soap:address location="https://localhost:4430/calculator_server.php?wsdl"/>
        </port>
    </service>
</definitions>
== Making the call ==
Could not connect to host

谢谢

所以显然代码不是问题,而是 SOAP 缓存。 一旦我使用ini_set("soap.wsdl_cache_enabled", 0);禁用它一切都开始工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM