我很喜欢我刚刚发现的Guzzle框架。我使用它来聚合使用不同响应结构的多个API的数据。它可以使用JSON和XML进行查找,但我需要使用的一个服务使用SOAP。有没有一种内置的方式来使用Guzzle服务?
发布于 2017-07-14 10:40:15
你可以让Guzzle发送SOAP请求。请注意,SOAP总是有一个信封、头和主体。
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<NormalXmlGoesHere>
<Data>Test</Data>
</NormalXmlGoesHere>
</soapenv:Body>我要做的第一件事是用SimpleXML构建XML体:
$xml = new SimpleXMLElement('<NormalXmlGoesHere xmlns="https://api.xyz.com/DataService/"></NormalXmlGoesHere>');
$xml->addChild('Data', 'Test');
// Removing xml declaration node
$customXML = new SimpleXMLElement($xml->asXML());
$dom = dom_import_simplexml($customXML);
$cleanXml = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);然后,我们用soap信封、头和正文包装xml主体。
$soapHeader = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>';
$soapFooter = '</soapenv:Body></soapenv:Envelope>';
$xmlRequest = $soapheader . $cleanXml . $soapFooter; // Full SOAP Request然后我们需要找出我们的端点在api文档中是什么。
然后我们在Guzzle中构建客户端:
$client = new Client([
'base_url' => 'https://api.xyz.com',
]);
try {
$response = $client->post(
'/DataServiceEndpoint.svc',
[
'body' => $xmlRequest,
'headers' => [
'Content-Type' => 'text/xml',
'SOAPAction' => 'https://api.xyz.com/DataService/PostData' // SOAP Method to post to
]
]
);
var_dump($response);
} catch (\Exception $e) {
echo 'Exception:' . $e->getMessage();
}
if ($response->getStatusCode() === 200) {
// Success!
$xmlResponse = simplexml_load_string($response->getBody()); // Convert response into object for easier parsing
} else {
echo 'Response Failure !!!';
}发布于 2014-02-13 14:59:40
IMHO Guzzle没有完全的SOAP支持,只能处理HTTP请求。src/Guzzle/Http/ClientInterface.php行:76
public function createRequest(
$method = RequestInterface::GET,
$uri = null,
$headers = null,
$body = null,
array $options = array()
); 即使SOAP服务器配置为在端口80上协商,我认为php SoapClient在这里是更合适的解决方案,因为它支持WSDL。
发布于 2016-08-31 16:24:31
老话题,但当我在搜索相同的答案时,似乎async-soap-guzzle正在做这项工作。
https://stackoverflow.com/questions/19170846
复制相似问题