Get the URL tag from enclosure XML
I want to get the URL from the enclosure tag with PHP
This is what i get from the RRS feed
<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>
This is the code im using now
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml_string);
foreach ($xmlDoc->getElementsByTagName('item') as $node) {
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'img' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue
);
echo "<pre>";
var_dump($item);
echo "</pre>";
}
And this is the result
array(2) {
["title"]=>
string(46) "Kettingbotsing met auto's en vrachtwagen op A2"
["img"]=>
string(10) "image/jpeg"
}
I'm currently getting the type of the enclosure tag, but i'm searching for the url.
Can someone help me,
Thanks in advance
php xml curl dom
add a comment |
I want to get the URL from the enclosure tag with PHP
This is what i get from the RRS feed
<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>
This is the code im using now
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml_string);
foreach ($xmlDoc->getElementsByTagName('item') as $node) {
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'img' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue
);
echo "<pre>";
var_dump($item);
echo "</pre>";
}
And this is the result
array(2) {
["title"]=>
string(46) "Kettingbotsing met auto's en vrachtwagen op A2"
["img"]=>
string(10) "image/jpeg"
}
I'm currently getting the type of the enclosure tag, but i'm searching for the url.
Can someone help me,
Thanks in advance
php xml curl dom
Possible duplicate of Get enclosure img url from rss feed
– JAAAY
Nov 21 '18 at 9:35
Possible duplicate of How do you parse and process HTML/XML in PHP?
– hanshenrik
Nov 21 '18 at 10:16
add a comment |
I want to get the URL from the enclosure tag with PHP
This is what i get from the RRS feed
<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>
This is the code im using now
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml_string);
foreach ($xmlDoc->getElementsByTagName('item') as $node) {
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'img' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue
);
echo "<pre>";
var_dump($item);
echo "</pre>";
}
And this is the result
array(2) {
["title"]=>
string(46) "Kettingbotsing met auto's en vrachtwagen op A2"
["img"]=>
string(10) "image/jpeg"
}
I'm currently getting the type of the enclosure tag, but i'm searching for the url.
Can someone help me,
Thanks in advance
php xml curl dom
I want to get the URL from the enclosure tag with PHP
This is what i get from the RRS feed
<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>
This is the code im using now
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml_string);
foreach ($xmlDoc->getElementsByTagName('item') as $node) {
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'img' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue
);
echo "<pre>";
var_dump($item);
echo "</pre>";
}
And this is the result
array(2) {
["title"]=>
string(46) "Kettingbotsing met auto's en vrachtwagen op A2"
["img"]=>
string(10) "image/jpeg"
}
I'm currently getting the type of the enclosure tag, but i'm searching for the url.
Can someone help me,
Thanks in advance
php xml curl dom
php xml curl dom
asked Nov 21 '18 at 9:13
Stan van HeertumStan van Heertum
459
459
Possible duplicate of Get enclosure img url from rss feed
– JAAAY
Nov 21 '18 at 9:35
Possible duplicate of How do you parse and process HTML/XML in PHP?
– hanshenrik
Nov 21 '18 at 10:16
add a comment |
Possible duplicate of Get enclosure img url from rss feed
– JAAAY
Nov 21 '18 at 9:35
Possible duplicate of How do you parse and process HTML/XML in PHP?
– hanshenrik
Nov 21 '18 at 10:16
Possible duplicate of Get enclosure img url from rss feed
– JAAAY
Nov 21 '18 at 9:35
Possible duplicate of Get enclosure img url from rss feed
– JAAAY
Nov 21 '18 at 9:35
Possible duplicate of How do you parse and process HTML/XML in PHP?
– hanshenrik
Nov 21 '18 at 10:16
Possible duplicate of How do you parse and process HTML/XML in PHP?
– hanshenrik
Nov 21 '18 at 10:16
add a comment |
3 Answers
3
active
oldest
votes
You need to use getAttribute()
instead of attributes
property
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
add a comment |
As an alternative to using DOMDocument, it's much clearer (IMHO) to use SimpleXML in this sort of case. The code ends up as...
$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>";
}
add a comment |
DOM supports Xpath expressions to fetch node lists and single values from an XML.
$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>";
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408637%2fget-the-url-tag-from-enclosure-xml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to use getAttribute()
instead of attributes
property
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
add a comment |
You need to use getAttribute()
instead of attributes
property
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
add a comment |
You need to use getAttribute()
instead of attributes
property
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
You need to use getAttribute()
instead of attributes
property
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
answered Nov 21 '18 at 9:18
MohammadMohammad
15.8k123664
15.8k123664
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
add a comment |
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
Yes thats it, thank you!
– Stan van Heertum
Nov 21 '18 at 9:20
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
@StanvanHeertum Glad to help you
– Mohammad
Nov 21 '18 at 9:21
add a comment |
As an alternative to using DOMDocument, it's much clearer (IMHO) to use SimpleXML in this sort of case. The code ends up as...
$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>";
}
add a comment |
As an alternative to using DOMDocument, it's much clearer (IMHO) to use SimpleXML in this sort of case. The code ends up as...
$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>";
}
add a comment |
As an alternative to using DOMDocument, it's much clearer (IMHO) to use SimpleXML in this sort of case. The code ends up as...
$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>";
}
As an alternative to using DOMDocument, it's much clearer (IMHO) to use SimpleXML in this sort of case. The code ends up as...
$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>";
}
answered Nov 21 '18 at 9:18
Nigel RenNigel Ren
28k61934
28k61934
add a comment |
add a comment |
DOM supports Xpath expressions to fetch node lists and single values from an XML.
$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>";
}
add a comment |
DOM supports Xpath expressions to fetch node lists and single values from an XML.
$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>";
}
add a comment |
DOM supports Xpath expressions to fetch node lists and single values from an XML.
$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>";
}
DOM supports Xpath expressions to fetch node lists and single values from an XML.
$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>";
}
answered Nov 21 '18 at 9:45
ThWThW
13.5k21633
13.5k21633
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408637%2fget-the-url-tag-from-enclosure-xml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Get enclosure img url from rss feed
– JAAAY
Nov 21 '18 at 9:35
Possible duplicate of How do you parse and process HTML/XML in PHP?
– hanshenrik
Nov 21 '18 at 10:16