2013-09-25 3 views
4

Я пытаюсь сделать запрос SOAP с помощью класса SoapClient из PHP Вот мой код:PHP SoapClient отправить пользовательские XML

$xmlstr = <<<XML 
<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://localhost:8090/mockCustomerManagement/types"> 
    <soapenv:Header> 
     <typ:customerManagementHeader> 
     <typ:userId>42</typ:userId> 
     <typ:requestId>1500</typ:requestId> 
     <typ:messageTimestamp>2013-09-25T14:31:21+00:00</typ:messageTimestamp> 
     </typ:customerManagementHeader> 
    </soapenv:Header> 
    <soapenv:Body> 
     <typ:getCustomerInfoData> 
     <useremail>[email protected]</useremail> 
     </typ:getCustomerInfoData> 
    </soapenv:Body> 
</soapenv:Envelope> 
XML; 

$wsdl = 'http://localhost:8090/mockCustomerManagementSoapHttpBinding?WSDL'; 
$client = new SoapClient($wsdl, array(
    'cache_wsdl' => WSDL_CACHE_NONE, 
    'cache_ttl'  => 86400, 
    'trace'   => true, 
    'exceptions' => true, 
)); 

$xmlVar = new SoapVar($xmlstr, XSD_ANYXML); 
$client->getCustomerInfo($xmlstr); 

Но просьба бросить меня исключение и когда я спрашиваю клиента для последнего запроса , это показывает некоторые дополнительный текст в начале и в конце моего XML, как вы можете видеть:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost:8090/mockCustomerManagement/types"> 
<SOAP-ENV:Body> 

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://localhost:8090/mockCustomerManagement/types"> 
    <soapenv:Header> 
     <typ:customerManagementHeader> 
     <typ:userId>42</typ:userId> 
     <typ:requestId>1500</typ:requestId> 
     <typ:messageTimestamp>2013-09-25T14:31:21+00:00</typ:messageTimestamp> 
     </typ:customerManagementHeader> 
    </soapenv:Header> 
    <soapenv:Body> 
     <typ:getCustomerInfoData> 
     <useremail>[email protected]</useremail> 
     </typ:getCustomerInfoData> 
    </soapenv:Body> 
</soapenv:Envelope> 

</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

Есть какой-то способ, чтобы удалить/отключить этот дополнительный текст? Другими словами, есть ли способ отправить только мой XML?

+0

звучит как это похоже, если не совсем такой же, как с предыдущие вопросы и ответы [отправка XML-кода через PHP SoapClient запрос] (http://stackoverflow.com/q/9608314/367456), поэтому перекрестная ссылка для ссылки. - +1 для Q & A. – hakre

ответ

5

Нет ничего лучше, чтобы задать вопрос, чтобы найти ответ. Я расширил класс SoapClient и, таким образом, отправил правильный XML. Это код:

/** 
* 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; 

Я использую бесплатную версию SOAP_UI для проверки ответа.

+0

Вы также можете удалить SoapEnvelope из XML, потому что он никогда не является частью параметра. – hakre

+4

Можете ли вы указать, как выглядит '$ XMLrequest' в контексте вашего примера? – Jacobian

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

// $ ответ показывает XML-ответ

как разобрать ответ мыло с использованием PHP для этого см следующий веб-сайт

https://vaja906programmingworld.wordpress.com/

Смежные вопросы