Php 从附件XML获取URL标记
我想用PHP从enclosure标签中获取URL 这是我从RRS提要中得到的Php 从附件XML获取URL标记,php,xml,curl,dom,Php,Xml,Curl,Dom,我想用PHP从enclosure标签中获取URL 这是我从RRS提要中得到的 <item> <title>Kettingbotsing met auto's en vrachtwagen op A2</title> <link>https://www.1limburg.nl/kettingbotsing-met-autos-en-vrachtwagen-op-a2</link> <desc
<item>
<title>Kettingbotsing met auto's en vrachtwagen op A2</title>
<link>https://www.1limburg.nl/kettingbotsing-met-autos-en-vrachtwagen-op-a2</link>
<description><p>Drie auto&#39;s en een vrachtauto zijn woensdagochtend met elkaar gebotst op de A2.&nbsp;&nbsp;</p></description>
<pubDate>Wed, 21 Nov 2018 07:37:56 +0100</pubDate>
<guid permalink="true">https://www.1limburg.nl/kettingbotsing-met-autos-en-vrachtwagen-op-a2</guid>
<enclosure type="image/jpeg" url="https://www.1limburg.nl/sites/default/files/public/styles/api_preview/public/image_16_13.jpg?itok=qWaZAJ8v" />
</item>
我目前正在获取附件标记的类型,但我正在搜索url
有人能帮我吗,
提前感谢您需要使用而不是属性
属性
$doc = simplexml_load_string($xml_string);
foreach ($doc->item as $node) {
$item = array(
'title' => (string)$node->title,
'img' => (string)$node->enclosure['url']
);
echo "<pre>";
var_dump($item);
echo "</pre>";
}
您需要使用而不是属性
属性
$doc = simplexml_load_string($xml_string);
foreach ($doc->item as $node) {
$item = array(
'title' => (string)$node->title,
'img' => (string)$node->enclosure['url']
);
echo "<pre>";
var_dump($item);
echo "</pre>";
}
作为使用DOMDocument的一种替代方法,在这种情况下使用SimpleXML更清晰(IMHO)。代码以
$document = new DOMDocument();
$document->loadXML($xml_string);
$xpath = new DOMXpath($document);
// iterate any item node in the document
foreach ($xpath->evaluate('//item') as $itemNode) {
$item = [
// first title child node cast to string
'title' => $xpath->evaluate('string(title)', $itemNode),
// first url attribute of an enclosure child node cast to string
'img' => $xpath->evaluate('string(enclosure/@url)', $itemNode)
];
echo "<pre>";
var_dump($item);
echo "</pre>";
}
$doc=simplexml\u load\u string($xml\u string);
foreach($doc->item as$node){
$item=数组(
'title'=>(字符串)$node->title,
'img'=>(字符串)$node->enclosure['url']
);
回声“;
var_dump(项目);
回声“;
}
作为使用DOMDocument的替代方法,在这种情况下使用SimpleXML更清晰(IMHO)。代码以
$document = new DOMDocument();
$document->loadXML($xml_string);
$xpath = new DOMXpath($document);
// iterate any item node in the document
foreach ($xpath->evaluate('//item') as $itemNode) {
$item = [
// first title child node cast to string
'title' => $xpath->evaluate('string(title)', $itemNode),
// first url attribute of an enclosure child node cast to string
'img' => $xpath->evaluate('string(enclosure/@url)', $itemNode)
];
echo "<pre>";
var_dump($item);
echo "</pre>";
}
$doc=simplexml\u load\u string($xml\u string);
foreach($doc->item as$node){
$item=数组(
'title'=>(字符串)$node->title,
'img'=>(字符串)$node->enclosure['url']
);
回声“;
var_dump(项目);
回声“;
}
DOM支持Xpath表达式从XML中获取节点列表和单个值
$document=新的DOMDocument();
$document->loadXML($xml\u字符串);
$xpath=newdomxpath($document);
//迭代文档中的任何项节点
foreach($xpath->evaluate('//item')为$itemNode){
$item=[
//转换为字符串的第一个标题子节点
'title'=>$xpath->evaluate('string(title)'$itemNode),
//转换为字符串的机柜子节点的第一个url属性
'img'=>$xpath->evaluate('string(enclosure/@url)',$itemNode)
];
回声“;
var_dump(项目);
回声“;
}
DOM支持Xpath表达式从XML中获取节点列表和单个值
$document=新的DOMDocument();
$document->loadXML($xml\u字符串);
$xpath=newdomxpath($document);
//迭代文档中的任何项节点
foreach($xpath->evaluate('//item')为$itemNode){
$item=[
//转换为字符串的第一个标题子节点
'title'=>$xpath->evaluate('string(title)'$itemNode),
//转换为字符串的机柜子节点的第一个url属性
'img'=>$xpath->evaluate('string(enclosure/@url)',$itemNode)
];
回声“;
var_dump(项目);
回声“;
}
@StanvanHeertum很乐意帮忙you@StanvanHeertum很高兴能帮助你可能的副本可能的副本可能的副本可能的副本
$document = new DOMDocument();
$document->loadXML($xml_string);
$xpath = new DOMXpath($document);
// iterate any item node in the document
foreach ($xpath->evaluate('//item') as $itemNode) {
$item = [
// first title child node cast to string
'title' => $xpath->evaluate('string(title)', $itemNode),
// first url attribute of an enclosure child node cast to string
'img' => $xpath->evaluate('string(enclosure/@url)', $itemNode)
];
echo "<pre>";
var_dump($item);
echo "</pre>";
}