繁体   English   中英

PHP Soap响应字符串数组

[英]PHP Soap Response String Array

这是我正在通过SOAPUI查看的SOAP对象:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetPatientsResponse xmlns="http://interface.sitename.com/api">
         <GetPatientsResult><![CDATA[<GetPatients FacilityId="test" ActiveOnly="True" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><PatientList><Patient>
      <Id>1b518b28-4781-40a0-8358-a4f801178885</Id>
      <LastName>Test123</LastName>
      <NickName xsi:nil="true" />
      <Gender>F</Gender>
      <Physician xsi:nil="true" />
      <IsActive>true</IsActive>
      <ArrivalDate>2015-08-18T00:00:00</ArrivalDate>
      <DepartureDate>2035-08-18T00:00:00</DepartureDate>
      <MedCartId>00000000-0000-0000-0000-000000000000</MedCartId>
      <TreatmentCartId>00000000-0000-0000-0000-000000000000</TreatmentCartId>
      <PictureUrl>https://interface.quickmar.com/beta/image_gen/pat1b518b28-4781-40a0-8358-a4f801178885.png</PictureUrl>
      <DOB>1900-01-01T00:00:00</DOB>
      <SSNLastDigits xsi:nil="true" />
      <FacilityPatientId xsi:nil="true" />
      <PharmacyPatientId xsi:nil="true" />
      <IsAssessmentNeeded>false</IsAssessmentNeeded>
      <IsRedFlagged>false</IsRedFlagged>
    </Patient>
    <Patient>
      <Id>642e39bc-d9e0-48e7-9c43-a4f801254b1e</Id>
      <FirstName>Allie</FirstName>
      <LastName>Assisted</LastName>
      <RoomNumber>302</RoomNumber>
      <NickName />
      <Gender>F</Gender>
      <Physician>Frank Jones</Physician>
      <IsActive>true</IsActive>
      <ArrivalDate>1999-12-15T00:00:00</ArrivalDate>
      <DepartureDate>2035-08-18T00:00:00</DepartureDate>
      <MedCartId>00000000-0000-0000-0000-000000000000</MedCartId>
      <TreatmentCartId>00000000-0000-0000-0000-000000000000</TreatmentCartId>
      <PictureUrl>https://interface.quickmar.com/beta/image_gen/pat642e39bc-d9e0-48e7-9c43-a4f801254b1e.png</PictureUrl>
      <DOB>1965-12-19T00:00:00</DOB>
      <SSNLastDigits>a1772</SSNLastDigits>
      <FacilityPatientId>1005</FacilityPatientId>
      <PharmacyPatientId xsi:nil="true" />
      <IsAssessmentNeeded>false</IsAssessmentNeeded>
      <IsRedFlagged>false</IsRedFlagged>
    </Patient>
    <Patient>
      <Id>98f0e2de-d165-4643-9c11-a4e900eb5818</Id>
      <FirstName>Charlie</FirstName>
      <LastName>Jones</LastName>
      <RoomNumber>101</RoomNumber>
      <NickName>Mr. Charlie</NickName>
      <Gender>M</Gender>
      <Allergies />
      <Diagnoses />
      <Physician />
      <IsActive>true</IsActive>
      <ArrivalDate>2011-04-04T00:00:00</ArrivalDate>
      <DepartureDate>2035-08-03T00:00:00</DepartureDate>
      <MedCartId>00000000-0000-0000-0000-000000000000</MedCartId>
      <TreatmentCartId>00000000-0000-0000-0000-000000000000</TreatmentCartId>
      <PictureUrl>https://interface.quickmar.com/beta/image_gen/pat98f0e2de-d165-4643-9c11-a4e900eb5818.png</PictureUrl>
      <DOB>1971-10-31T00:00:00</DOB>
      <SSNLastDigits xsi:nil="true" />
      <FacilityPatientId xsi:nil="true" />
      <PharmacyPatientId xsi:nil="true" />
      <IsAssessmentNeeded>false</IsAssessmentNeeded>
      <Diet />
      <IsRedFlagged>false</IsRedFlagged>
    </Patient></PatientList></GetPatients>]]></GetPatientsResult>
      </GetPatientsResponse>
   </soap:Body>
</soap:Envelope>

我的PHP代码:

$soapClient = new SoapClient('http://remoteapi.asmx?wsdl');
$params = array('facilityId' => 'test',
                'authId'     => 'biglongauthcode',
                'activeOnly' => true);

$response = $soapClient->GetPatients($params);

如果我执行:

var_dump($response);

我得到:

object(stdClass)#2 (1) { ["GetPatientsResult"]=> string(2944) " 1b518b28-4781-40a0-8358-a4f801178885 Test123 F true 2015-08-18T00:00:00 2035-08-18T00:00:00 
00000000-0000-0000-0000-000000000000 00000000-0000-0000-0000-000000000000 https://interface.api.com/beta/image_gen/pat1b518b28-4781-40a0-8358-a4f801178885.png 
1900-01-01T00:00:00 false false 642e39bc-d9e0-48e7-9c43-a4f801254b1e Allie Assisted 302 F Frank Jones true 1999-12-15T00:00:00 2035-08-18T00:00:00 00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000 https://interface.api.com/beta/image_gen/pat642e39bc-d9e0-48e7-9c43-a4f801254b1e.png 1965-12-19T00:00:00 a1772 1005 false false 98f0e2de-
d165-4643-9c11-a4e900eb5818 Charlie Jones 101 Mr. Charlie M true 2011-04-04T00:00:00 2035-08-03T00:00:00 00000000-0000-0000-0000-000000000000 00000000-0000-0000-0000-000000000000 
https://interface.api.com/beta/image_gen/pat98f0e2de-d165-4643-9c11-a4e900eb5818.png 1971-10-02T00:00:00 false false " }

我还运行了以下代码:

foreach ($response as $key => $value) {
    $xml = htmlentities($value);
}
echo "<pre>".$xml;

这仅迭代一次,因为整个数据集都在单个数组值中。 上面的输出是:

<GetPatients FacilityId="test" ActiveOnly="True" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><PatientList><Patient>
      <Id>1b518b28-4781-40a0-8358-a4f801178885</Id>
      <LastName>Test123</LastName>
      <NickName xsi:nil="true" />
      <Gender>F</Gender>
      <Physician xsi:nil="true" />
      <IsActive>true</IsActive>
      <ArrivalDate>2015-08-18T00:00:00</ArrivalDate>
      <DepartureDate>2035-08-18T00:00:00</DepartureDate>
      <MedCartId>00000000-0000-0000-0000-000000000000</MedCartId>
      <TreatmentCartId>00000000-0000-0000-0000-000000000000</TreatmentCartId>
      <PictureUrl>https://interface.api.com/beta/image_gen/pat1b518b28-4781-40a0-8358-a4f801178885.png</PictureUrl>
      <DOB>1900-01-01T00:00:00</DOB>
      <SSNLastDigits xsi:nil="true" />
      <FacilityPatientId xsi:nil="true" />
      <PharmacyPatientId xsi:nil="true" />
      <IsAssessmentNeeded>false</IsAssessmentNeeded>
      <IsRedFlagged>false</IsRedFlagged>
    </Patient>
    <Patient>
      <Id>642e39bc-d9e0-48e7-9c43-a4f801254b1e</Id>
      <FirstName>Allie</FirstName>
      <LastName>Assisted</LastName>
      <RoomNumber>302</RoomNumber>
      <NickName />
      <Gender>F</Gender>
      <Physician>Frank Jones</Physician>
      <IsActive>true</IsActive>
      <ArrivalDate>1999-12-15T00:00:00</ArrivalDate>
      <DepartureDate>2035-08-18T00:00:00</DepartureDate>
      <MedCartId>00000000-0000-0000-0000-000000000000</MedCartId>
      <TreatmentCartId>00000000-0000-0000-0000-000000000000</TreatmentCartId>
      <PictureUrl>https://interface.api.com/beta/image_gen/pat642e39bc-d9e0-48e7-9c43-a4f801254b1e.png</PictureUrl>
      <DOB>1965-12-19T00:00:00</DOB>
      <SSNLastDigits>a1772</SSNLastDigits>
      <FacilityPatientId>1005</FacilityPatientId>
      <PharmacyPatientId xsi:nil="true" />
      <IsAssessmentNeeded>false</IsAssessmentNeeded>
      <IsRedFlagged>false</IsRedFlagged>
    </Patient>
    <Patient>
      <Id>98f0e2de-d165-4643-9c11-a4e900eb5818</Id>
      <FirstName>Charlie</FirstName>
      <LastName>Jones</LastName>
      <RoomNumber>101</RoomNumber>
      <NickName>Mr. Charlie</NickName>
      <Gender>M</Gender>
      <Allergies />
      <Diagnoses />
      <Physician />
      <IsActive>true</IsActive>
      <ArrivalDate>2011-04-04T00:00:00</ArrivalDate>
      <DepartureDate>2035-08-03T00:00:00</DepartureDate>
      <MedCartId>00000000-0000-0000-0000-000000000000</MedCartId>
      <TreatmentCartId>00000000-0000-0000-0000-000000000000</TreatmentCartId>
      <PictureUrl>https://interface.quickmar.com/beta/image_gen/pat98f0e2de-d165-4643-9c11-a4e900eb5818.png</PictureUrl>
      <DOB>1971-10-02T00:00:00</DOB>
      <SSNLastDigits xsi:nil="true" />
      <FacilityPatientId xsi:nil="true" />
      <PharmacyPatientId xsi:nil="true" />
      <IsAssessmentNeeded>false</IsAssessmentNeeded>
      <Diet />
      <IsRedFlagged>false</IsRedFlagged>
    </Patient></PatientList></GetPatients>

问题是我不确定自己在做什么错。 我想通过使用以下代码对$ response对象进行操作:

$lname = $response->Patient->Id

或以任何方式组合,我已经花了很多小时没有任何工作,我意识到一旦将$ response对象正确地分解为键值对,我将回到混乱的操作方法数据,但是现在它就像一个巨大的字符串,我无法从中得到任何东西。

任何帮助,将不胜感激。 我搜索了几个小时,没有看到其他人遇到的所有数据都在单个数组值中的问题。

我希望这会在将来对其他人有所帮助,但这是解决方案:

您要替换:

foreach ($response as $key => $value) {
    $xml = htmlentities($value);
}

带有:

foreach ($response as $key => $value) {
    $xml = html_entity_decode($value);

这将删除所有html标记,并为您提供一个干净的XML文件。 为了能够解析文件,请使用simpleXML,如下所示:

$residents = simplexml_load_string($xml);

现在,您可以像访问数组一样访问数据。 例如,如果要打印第一位患者的Id属性的值,则只需使用以下代码行:

echo "First Resident ID: ".$residents->PatientList->Patient[0]->Id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM