使用Guzzle来使用SOAP

时间:2013-10-03 23:35:06

标签: soap guzzle

我喜欢刚刚发现的Guzzle框架。我正在使用它来使用不同的响应结构在多个API之间聚合数据。它使用JSON和XML查找,但我需要使用的服务之一使用SOAP。是否有一种内置的方式来使用Guzzle来使用SOAP服务?

4 个答案:

答案 0 :(得分:7)

你可以让Guzzle发送SOAP请求。 请注意,SOAP始终具有Envelope,Header和Body。

<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);

然后我们用肥皂信封,标题和正文包裹我们的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 !!!';
}

答案 1 :(得分:4)

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

答案 2 :(得分:4)

旧主题,但在我寻找相同的答案时,似乎async-soap-guzzle正在完成这项工作。

答案 3 :(得分:1)

Guzzle HTTP可以用于SOAP请求,并且具有如下魅力:

下面是我的实现方式。

创建变量:

    public function __construct(Request $request) {
    $this->request = $request;
    $this->openSoapEnvelope   =   '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">';
    $this->soapHeader         =   '<soap:Header> 
                                    <tem:Authenticate>  
                                        <-- any header data goes here-->
                                    </tem:Authenticate>
                                </soap:Header>';

    $this->closeSoapEnvelope   =   '</soap:Envelope>';
}

创建一个函数以形成一个肥皂请求。

public function generateSoapRequest($soapBody){
    return $this->openSoapEnvelope . $this->soapHeader . $soapBody . $this->closeSoapEnvelope;
}

定义主体并调用generateSoapRequest方法。 例如:

$soapBody           =   '<soap:Body>
                                <tem:GetSomeDetails/>
                          </soap:Body>';

$xmlRequest         =   $this->generateSoapRequest($soapBody); 


$client = new Client();

        $options = [
            'body'    => $xmlRequest,
            'headers' => [
                "Content-Type" => "text/xml",
                "accept" => "*/*",
                "accept-encoding" => "gzip, deflate"
            ]
        ];

        $res = $client->request(
            'POST',
            'http://your-soap-endpoint-url',
            $options
        );

        print_r($res->getBody());