我正在尝试使用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模式下使用webservice,它可以正常工作,但我需要使用WSDL(我在另一端用另一个PHP脚本生成)。
而且,如果您想知道,WSDL似乎很好,因为我用SoapUI检查了它。
谢谢你的帮助
,
编辑:
我试着这个小python脚本:
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和服务器返回的响应……我查了旧版本的问候语。WSDL文件,发现了这个神秘的"问候"。元素。我猜一定有缓存问题,但我不能把我的手指在它。
我使用的python库是this
我终于得到了我的答案…这是一个困难的问题,我甚至不确定我是如何发现它的,但问题是在WSDL定义中。
更具体地说,绑定样式是错误的(或者至少与我使用的客户端不兼容)。
原来的是document
,我需要它是rpc
。
一旦我做了这个小小的改变,一切都像预期的那样工作了。
我给别人留下了我的代码参考。
编辑:为了记录,这个网站的例子是一个提示我:)