如何通过restful URL搜索PHP中的XML

时间:2017-03-20 10:00:47

标签: php xml restful-url

我从curl获得了XML响应并试图搜索某些标记值,但我失败了。请有人帮助我

请不要因为我是PHP的新手

<?php
$DynamicValue = 'KWI';
$url='https://www.example.com/_api/web/lists/getbytitle(\'Stations\')/items?$select=OfficeId&$filter='.urlencode("CityCode eq '" . $DynamicValue . "'");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$XML = new SimpleXMLElement($result);
foreach($XML->entry as $value)
{
    echo $value->OfficeId
}

输出:

<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>a9f073a2-387e-4ac5-81b9-16b3ae81dba3</id><title /><updated>2017-03-20T08:15:47Z</updated><entry m:etag="&quot;61&quot;"><id>Web/Lists(guid'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9')/Items(23)</id><category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/Lists(guid'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9')/Items(23)" /><title /><updated>2017-03-20T08:15:47Z</updated><author><name /></author><content type="application/xml"><m:properties><d:OfficeId>KWIKU08AA</d:OfficeId></m:properties></content></entry></feed>

1 个答案:

答案 0 :(得分:1)

希望这会奏效。

$string='<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>a9f073a2-387e-4ac5-81b9-16b3ae81dba3</id><title /><updated>2017-03-20T08:15:47Z</updated><entry m:etag="&quot;61&quot;"><id>Web/Lists(guid\'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9\')/Items(23)</id><category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/Lists(guid\'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9\')/Items(23)" /><title /><updated>2017-03-20T08:15:47Z</updated><author><name /></author><content type="application/xml"><m:properties><d:OfficeId>KWIKU08AA</d:OfficeId></m:properties></content></entry></feed>';
echo explode("</d:OfficeId>",explode("<d:OfficeId>", $string)[1])[0];

PHP Code demo