Using Guzzle to consume SOAP

后端 未结 4 1948
夕颜
夕颜 2021-02-12 04:24

I\'m loving the Guzzle framework that I just discovered. I\'m using it to aggregate data across multiple API\'s using different response structures. It\'s worked find with JSON

4条回答
  •  时光说笑
    2021-02-12 05:27

    Guzzle HTTP can be used for SOAP requests & works like a charm:

    Below is the way I have implemented.

    Create variables:

        public function __construct(Request $request) {
        $this->request = $request;
        $this->openSoapEnvelope   =   '';
        $this->soapHeader         =   ' 
                                          
                                            <-- any header data goes here-->
                                        
                                    ';
    
        $this->closeSoapEnvelope   =   '';
    }
    

    Create a function to form a soap request.

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

    Define a body & call generateSoapRequest method. e.g:

    $soapBody           =   '
                                    
                              ';
    
    $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());
    

提交回复
热议问题