2013-10-03 26 views
14

私は今発見したGuzzleフレームワークを愛しています。私はそれを使用して、さまざまな応答構造を使用して複数のAPIにまたがってデータを集計しています。 JSONとXMLを使って見つけることができますが、消費するために必要なサービスの1つにSOAPを使用しています。 GuzzleでSOAPサービスを使用する組み込みの方法はありますか?Guzzleを使用してSOAPを消費する

+0

私もこのトピックに関する詳細情報を取得したいと思います。 Guzzleのドキュメントには、.wsdlファイルやSOAPに関する記述はありません。 – Rvanlaak

答えて

4

IMHO完全なSOAPサポートがなく、HTTPリクエストでのみ動作します。 のsrc /がつがつ食う/ HTTP/ClientInterface.phpライン:76

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

SOAPサーバは、ポート80上で交渉するように設定されている場合でも、私はそれがWSDL

+0

私は今やバージョン6になっていると思います。彼らがSOAPをサポートするために何らかの変更を行ったかどうか知っていますか? – gmponos

5

古いトピックをサポートしているとして、PHPののSoapClientはこちらより適切な解決策だと思います私は同じ答えを探していたので、async-soap-guzzleが仕事をしているようです。

2

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ドキュメントにあるかどうかを調べる必要があります。

私たちは、その後、がつがつ食うにクライアントを構築:

$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 !!!'; 
} 
関連する問題