PHP soapClient send custom XML

后端 未结 2 491
旧巷少年郎
旧巷少年郎 2021-01-05 20:48

I\'m trying to make a SOAP request using soapClient class from PHP that\'s my code:

$xmlstr = <<

        
相关标签:
2条回答
  • 2021-01-05 20:56
    // xml post structure
            $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
                                <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:Header>
                                        <AuthHeader xmlns="http://tempuri.org/">
                                          <UserName>username</UserName>
                                          <Password>password</Password>
                                        </AuthHeader>
                                      </soap:Header>
                                      <soap:Body>
                                        <webservice_method_name xmlns="http://tempuri.org/" />
                                      </soap:Body>
                                    </soap:Envelope>';   
               $headers = array(
                            "Content-type: text/xml;charset=\"utf-8\"",
                            "Accept: text/xml",
                            "Cache-Control: no-cache",
                            "Pragma: no-cache",
                            "SOAPAction:http://tempuri.org/webservice_method_name", 
                            "Content-length: ".strlen($xml_post_string),
                        );
                $url =  "http://yourwebserviceurl.com/servicename.asmx";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                $response = curl_exec($ch); 
                curl_close($ch);
    

    //$response showing XML Response

    how to parse soap response using php for that see following website

    https://vaja906programmingworld.wordpress.com/

    0 讨论(0)
  • 2021-01-05 21:18

    There is nothing like make an question to find the answer. I extended the SoapClient class and, in this way, I send the correct XML. That's the code:

    /**
     * Extend SoapClientClass
     */
    class anotherSoapClient extends SoapClient {
    
        function __construct($wsdl, $options) {
            parent::__construct($wsdl, $options);
            $this->server = new SoapServer($wsdl, $options);
        }
        public function __doRequest($request, $location, $action, $version) { 
            $result = parent::__doRequest($request, $location, $action, $version); 
            return $result; 
        } 
        function __anotherRequest($call, $params) {
            $location = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding';
            $action = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding/'.$call;
            $request = $params;
            $result =$this->__doRequest($request, $location, $action, '1');
            return $result;
        } 
    }
    
    // Create new SOAP client
    $wsdl = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding?WSDL';
    $client = new anotherSoapClient($wsdl, array(
        'cache_wsdl'    => WSDL_CACHE_NONE, 
        'cache_ttl'     => 86400, 
        'trace'         => true,
        'exceptions'    => true,
    ));
    
    // Make the request
    try {
        $request = $client->__anotherRequest('getCustomerInfo', $XMLrequest);
    } catch (SoapFault $e ){
        echo "Last request:<pre>" . htmlentities($client->__getLastRequest()) . "</pre>";
        exit();
    }
    
    header('Content-type: text/xml');
    echo $request;
    

    I use the free version of SOAP_UI to test the response.

    0 讨论(0)
提交回复
热议问题