web-dev-qa-db-ja.com

Guzzleを使用して消費するSOAP

今発見したGuzzleフレームワークが大好きです。さまざまな応答構造を使用して、複数のAPI間でデータを集約するために使用しています。 JSONとXMLを使用して機能しますが、使用する必要があるサービスの1つはSOAPを使用します。 GuzzleでSOAPサービスを利用するための組み込みの方法はありますか?

22
LordZardeck

SOAPリクエストを送信するようにGuzzleを取得できます。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);

次に、XML本体をSOAPエンベロープ、ヘッダー、および本体でラップします。

$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 !!!';
}
11
alakin_11

古いトピックですが、同じ答えを探していたので、 async-soap-guzzle が仕事をしているようです。

4
Raphael1px

IMHO Guzzleは完全なSOAPサポートを備えておらず、HTTPリクエストでのみ機能します。src/ Guzzle/Http/ClientInterface.php Line:76

public function createRequest(                                              
    $method = RequestInterface::GET,                                        
    $uri = null,                                                            
    $headers = null,                                                        
    $body = null,                                                           
    array $options = array()                                                
); 

SOAP=サーバーがポート80でネゴシエートするように構成されている場合でも、ここではWSDLをサポートしているため、php SoapClientがより適切なソリューションだと思います

4
ishenkoyv

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>';
}

SOAPリクエストを形成する関数を作成します。

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());
2
user3785966