PHP 中的 SOAP 请求和响应

SOAP request and response in PHP

提问人:Sergei 提问时间:1/9/2023 更新时间:1/9/2023 访问量:81

问:

我对这个问题绝对是新手。我需要从PHP中的SOAP请求中获取令牌。

请求

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/"> 
       <soap:Header/> 
       <soap:Body> 
          <tem:GetToken> 
             <tem:WebChanelID>8</tem:WebChanelID> 
             <tem:AccessCode>444555</tem:AccessCode> 
          </tem:GetToken> 
       </soap:Body> 
    </soap:Envelope>

服务器 https://hma.ecvi.ru/service/webform.wsdl.php

PHP Web 服务 SOAP

评论

0赞 Buttered_Toast 1/9/2023
这是一种 xml 格式,因此您需要使用 XML 解析器,或者如果您只想要一个属性,请使用正则表达式,请检查此 regex101.com/r/Brz2WF/1
0赞 Sergei 1/9/2023
我为我找到了一些解决方案(以下),但它不起作用。我做错了什么?

答:

0赞 Sergei 1/9/2023 #1

我在这里做错了什么?

$soapUrl = "https://hma.ecvi.ru/service/webform.wsdl.php";

$xml_post_string = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/"> 
   <soap:Header/> 
   <soap:Body> 
      <tem:GetToken> 
         <tem:WebChanelID>631</tem:WebChanelID> 
         <tem:AccessCode>989</tem:AccessCode> 
      </tem:GetToken> 
   </soap:Body> 
</soap:Envelope>';

$headers = array(
"POST /package/package_1.3/packageservices.asmx HTTP/1.1",
"Host: hma.ecvi.ru",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
curl_close($ch);

$response1 = str_replace("<soap:Body>","",$response);
    $response2 = str_replace("</soap:Body>","",$response1);

$parser = simplexml_load_string($response2);
echo $parser;

评论

0赞 Martin54 1/9/2023
您需要更新您的问题。没有人会检查你的答案。