2017-03-20 29 views
1

我已经从curl获得XML响应并试图搜索某些标记值,但是我失败了。请有人帮助我如何在PHP中搜索XML通过宁静的URL得到

请B-如我是新来的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

希望这会工作。

$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

+0

不,实际上我想搜索 KWIKU08AA的价值如何能做到这一点 – Milind

+0

@Milind现在检查 –