php - 如何制作 PHP SOAP 客户端并将结果 xml 存储在 php 变量中

标签 php web-services soap wsdl

我正在使用 PHP,之前从未使用过 SOAP 和 PHP。

我需要构建一个 Soap PHP 客户端,它从 Soap 服务器 .NET Web 服务

我目前正在为荷兰医疗保健系统中的医生获取信息 .对于在荷兰医疗保健系统中注册的每位医生,您都可以检索 他的信息通过他唯一的 unique BIG ID - 11 位唯一数字 )来自 使用 WSDL 的 SOAP 网络服务

所以当我调用 SOAP 服务器 时: (服务器链接如下)

进入这个测试网站(比如 soapclinet.com)

我的 XML 响应是正确的,看起来与下面的 XML 完全一样 括号中是我想在 php 中捕获的医生信息。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
 <ListHcpApprox3Result xmlns="http://services.cibg.nl/ExternalUser">
  <ListHcpApprox>
   <ListHcpApprox3>
    <HcpNumber>200822</HcpNumber>
    <BirthSurname> [Surname of the Doctor]</BirthSurname>
    <MailingName> [Mailing name of the doctor] </MailingName>
    <Initial> [Initials of the doctor] </Initial>
    <Gender> [Gender of the doctor] </Gender>
    <ArticleRegistration>
    <ArticleRegistrationExtApp>
    <ArticleRegistrationNumber> [unique BIG ID]  </ArticleRegistrationNumber>
    <StartDate>1998-12-10T00:00:00</StartDate>
    <EndDate xsi:nil="true"/>
    <TypeOfSpecialismId>15</TypeOfSpecialismId>
    </SpecialismExtApp>
    </Specialism>
    <Mention/>
    <JudgmentProvision/>
    <Limitation/>
   </ListHcpApprox3>
  </ListHcpApprox>
 </ListHcpApprox3Result>
</soap:Body>
</soap:Envelope>

我需要构建一个 PHP 网页,它将执行完全相同的 SOAP 调用。

wsdl文件的地址是这样的:

webservices.cibg.nl/Ribiz/OpenbaarV2.asmx?wsdl.asmx?wsdl

soap 服务器地址是这样的: http://webservices-acc.cibg.nl/Ribiz/OpenbaarV2.asmx

SOAP 作用是这样的: http://services.cibg.nl/ExternalUser/ListHcpApprox3

我发送的 SOAP 消息是这样的:

<?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:Body>
 <listHcpApproxRequest xmlns="http://services.cibg.nl/ExternalUser">
  <WebSite>Ribiz</WebSite>
  <RegistrationNumber> [BIG ID number of the doctor] </RegistrationNumber>
 </listHcpApproxRequest>
</soap:Body>
</soap:Envelope>

(括号中是我们在SOAP服务器中发送的医生的BIG ID号)

我如何将上述 SOAP 操作写入 PHP 代码并将我作为响应获得的 XML 变量存储在 PHP 中?

我需要在 php 代码中存储的 XML 响应中的变量是这些......

<HcpNumber>200822</HcpNumber>
<BirthSurname> [Surname of the Doctor]</BirthSurname>
<MailingName> [Mailing name of the doctor] </MailingName>
<Initial> [Initials of the doctor] </Initial>
<Gender> [Gender of the doctor] </Gender> 

Update1:​​ 这是 var_dump 的输出。其中 xxxxxxx 是我要存储在 php 变量中的值!

object(stdClass)[2]
  public 'ListHcpApprox' => 
    object(stdClass)[3]
      public 'ListHcpApprox3' => 
        object(stdClass)[4]
          public 'HcpNumber' => string 'xxxxxxxxx' (length=6)
          public 'BirthSurname' => string 'xxxxxxxxxxx' (length=9)
          public 'MailingName' => string 'xxxxxxxxxxxxxxxxxx' (length=18)
          public 'Initial' => string 'xxxxxxxxxxxx' (length=8)
          public 'Gender' => string 'x' (length=1)
          public 'ArticleRegistration' => 
            object(stdClass)[5]
              ...
          public 'Specialism' => 
            object(stdClass)[7]
              ...
          public 'Mention' => 
            object(stdClass)[9]
              ...
          public 'JudgmentProvision' => 
            object(stdClass)[10]
              ...
          public 'Limitation' => 
            object(stdClass)[11]
              ...

最佳答案

PHP 有一个原生的 SoapClient使调用 SOAP 服务变得容易的类。它提供了一个简单的接口(interface),允许您将 native PHP 数组和对象用于请求和响应数据,并处理 SOAP 信封和 WSDL 的复杂性。

// create a new SoapClient and pass it the WSDL
$client = new SoapClient('http://webservices.cibg.nl/Ribiz/OpenbaarV2.asmx?WSDL');

// define the input parameters for the webservice call
$params = array('WebSite' => 'Ribiz', 'RegistrationNumber' => 'xxxxxxxxxx');
// invoke the ListHcpApprox3 method
$response = $client->ListHcpApprox3($params);

// print out the response to see its structure
var_dump($response);

您可以像这样从响应中挑选变量:

$data = $response->ListHcpApprox->ListHcpApprox3
$HcpNumber = $data->HcpNumber;
$BirthSurname = $data->BirthSurname;

echo $HcpNumber;

关于php - 如何制作 PHP SOAP 客户端并将结果 xml 存储在 php 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17567424/

相关文章:

javascript - setInterval 使用 ajax 和特定的目标函数

javascript - php 的 $_POST 中的 angularJs POST 数据

java - MyEclipse 不读取导入的证书

php - 如何使用php通过https下载文件

php - 如何制作以半小时为间隔的时间数组?

Android 异步消费 Web 服务

Java:在浏览器中显示一些 SQL 数据并进行交互的简单方法

c# - 调用 Web 服务时出现问题 - 使用 XmlInclude 或 SoapInclude 属性

java - 用于基于 Java 的 SOAP Web 服务的 PHP SOAP XML 格式

grails - 如何在 groovy 中使用 cxf 制作 map ?