Web Services 如何使用WSDL模式通过PHP使用Web服务

fv2wmkja  于 2022-11-15  发布在  PHP
关注(0)|答案(1)|浏览(221)

我正在尝试使用PHP使用Web服务。
这是我的代码:

<?php

ini_set('soap.wsdl_cache_enabled', false);

class SayHelloResponse
{
    public $sayHelloResult;
    public $greeting;
}

$client = new SoapClient(
    'http://localhost:8000/greetings_server.php?wsdl', [
        'trace' => 1,
        'soap_version' => SOAP_1_2,
        'classmap' => [
            'sayHelloResponse' => SayHelloResponse::class,
        ]
    ]);

if ($argc === 1) {
    echo "Service description:" . PHP_EOL;
    echo "--------------------" . PHP_EOL;
    echo "Functions:" . PHP_EOL;
    print_r($client->__getFunctions());
    echo "=====" . PHP_EOL;
    echo "Types:" . PHP_EOL;
    print_r($client->__getTypes());
} elseif ($argv[1] == "h") {
    try {
        echo "Making the call" . PHP_EOL;
        $sayHelloResponse = $client->sayHello(
            [
                'name' => $argv[2]
            ]);

        var_dump($sayHelloResponse);
        var_dump($client->__getLastResponse());
    } catch (SoapFault $exception) {
        echo "Error: " . $exception->getMessage() . PHP_EOL;
        var_dump($client->__getLastRequest());
        var_dump($client->__getLastResponse());
    }
} else {
    echo $client->sayGoodBye($argv[2]);
}

当我在没有任何CLI参数的情况下运行脚本时,我会得到:

Service description:
--------------------
Functions:
Array
(
    [0] => sayHelloResponse sayHello(sayHello $parameters)
    [1] => sayGoodByeResponse sayGoodBye(sayGoodBye $parameters)
)
=====
Types:
Array
(
    [0] => struct sayHello {
 string name;
}
    [1] => struct sayHelloResponse {
 string sayHelloResult;
}
    [2] => struct sayGoodBye {
 string name;
}
    [3] => struct sayGoodByeResponse {
 string sayGoodByeResult;
}
)

然后,当我用php greetings.php h Mauro运行它时,我得到:

Making the call
object(SayHelloResponse)#2 (2) {
  ["sayHelloResult"]=>
  NULL
  ["greeting"]=>
  NULL
}
string(559) "<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:Greetings" 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 xmlns:rpc="http://www.w3.org/2003/05/soap-rpc"><ns1:sayHelloResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding"><rpc:result>greeting</rpc:result><greeting xsi:type="xsd:string">Hello Mauro!</greeting></ns1:sayHelloResponse></env:Body></env:Envelope>
"

所以我可以看到我正在寻找的结果就在那里(Hello Mauro!),我只是不知道如何访问它:(
问题是,如果我尝试在非wsdl模式下使用Web服务,它可以正常工作,但我需要使用WSDL(我在另一端用另一个PHP脚本生成)。
而且,如果您想知道,WSDL似乎是好的,因为我检查了SoapUI。
谢谢你的帮助

编辑:
我试了一下小蟒的脚本:

from zeep import Client
client = Client('http://localhost:8000/greetings_server.php?wsdl')
result = client.GreetingsService.sayHello('Mauro')

当我运行它时,我得到:

Traceback (most recent call last):
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/xsd/types/complex.py", line 206, in parse_xmlelement
    result = element.parse_xmlelements(
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/xsd/elements/indicators.py", line 617, in parse_xmlelements
    item_subresult = element.parse_xmlelements(
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/xsd/elements/element.py", line 206, in parse_xmlelements
    raise UnexpectedElementError(
zeep.exceptions.UnexpectedElementError: Unexpected element 'greeting', expected 'sayHelloResult'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/proxy.py", line 46, in __call__
    return self._proxy._binding.send(
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/wsdl/bindings/soap.py", line 135, in send
    return self.process_reply(client, operation_obj, response)
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/wsdl/bindings/soap.py", line 231, in process_reply
    result = operation.process_reply(doc)
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/wsdl/bindings/soap.py", line 430, in process_reply
    return self.output.deserialize(envelope)
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/wsdl/messages/soap.py", line 101, in deserialize
    body_result = self._deserialize_body(body)
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/wsdl/messages/soap.py", line 442, in _deserialize_body
    result = self.body.parse(xmlelement, self.wsdl.types, context=context)
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/xsd/elements/element.py", line 126, in parse
    return xsd_type.parse_xmlelement(
  File "/home/mauro/.local/lib/python3.8/site-packages/zeep/xsd/types/complex.py", line 212, in parse_xmlelement
    raise XMLParseError(exc.message)
zeep.exceptions.XMLParseError: Unexpected element 'greeting', expected 'sayHelloResult'

从这里我发现WSDL和服务器返回的响应不匹配......我检查了一个旧版本的greetings.wsdl文件,发现了这个奇怪的“greeting”元素。我猜一定是缓存问题,但我不能确定是什么。
我使用的python库是this

p5cysglq

p5cysglq1#

我终于得到了我的答案......这是一个困难的问题,我甚至不知道我是如何发现它的,但问题出在WSDL定义中。
更具体地说,绑定样式是错误的(或者至少与我正在使用的客户机不兼容)。
原来的一个是document,我需要它是rpc
一旦我做了那个小改变,一切都如预期的那样工作。
我留下一个reference to my code给其他人看。
编辑:声明一下,this site's example是给我通风报信的人:)

相关问题