PHP Soap响应字符串数组

时间:2015-08-19 10:52:50

标签: php soap

这是我通过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对象正确分解为键值对,我将回到混乱状态处理数据的方法,但现在它就像一个巨大的字符串,我无法从中获取任何东西。

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

1 个答案:

答案 0 :(得分:0)

我希望将来可以帮助其他人,但这是解决方案:

您想要替换:

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;