Php Rss feed use img in CDATA -> content:encoded - php

I need to display only the image from the CDATA content.
<item>
<title>... </title>
<link>... </link>
<description>... </description>
<category>... </category>
<pubDate>... </pubDate>
<guid>... </guid>
<content:encoded><![CDATA[<img alt="" src="..."/>...]]></content:encoded>
<enclosure url="..." type="image/jpeg" length="171228"></enclosure>
</item>
my code i tried looks like this an works fine for the part
<?php
$html = "";
$url = "http://www....";
$xml = simplexml_load_file($url);
for($i = 0; $i < 1; $i++){
//works fine with <description> part
$description = $xml->channel->item[$i]->description;
preg_match("/<img[^>]+\>/i", $description, $matches);
if (isset($matches[0])) {
$html .= $matches[0];
//echo "<br/>show img<br/>";
} else {
$html .= $description;
echo "<br/>there is no img";
}
}
echo $html;
?>
now i need a method for the part and show the image separately.
can someone help me fix this?
maybe it is possible to use the url in to display the image.

Related

Read colon tags values XML PHP

I've already read those topics:
PHP library for parsing XML with a colons in tag names? and
Simple XML - Dealing With Colons In Nodes but i coundt implement those solutions.
<item>
<title> TITLE </title>
<itunes:author> AUTHOR </itunes:author>
<description> TEST </description>
<itunes:subtitle> TEST </itunes:subtitle>
<itunes:summary> TEST </itunes:summary>
<itunes:image href="yoyoyoyo.jpg"/>
<pubDate> YESTERDAY </pubDate>
<itunes:block>no</itunes:block>
<itunes:explicit>no</itunes:explicit>
<itunes:duration>99:99:99</itunes:duration>
<itunes:keywords>key, words</itunes:keywords>
</item>
I want to get only itunes:duration and itunes:image. Here is my code:
$result = simplexml_load_file("http://blablabla.com/feed.xml");
$items = $result->xpath("//item");
foreach ($items as $item) {
echo $item->title;
echo $item->pubDate;
}
I tried using children() method but when i try to print_r it it says that the node no longer exists.
You should use the children() on the $item element to get it's child-elements:
$str =<<< END
<item>
<title> TITLE </title>
<itunes:author> AUTHOR </itunes:author>
<description> TEST </description>
<itunes:subtitle> TEST </itunes:subtitle>
<itunes:summary> TEST </itunes:summary>
<itunes:image href="yoyoyoyo.jpg"/>
<pubDate> YESTERDAY </pubDate>
<itunes:block>no</itunes:block>
<itunes:explicit>no</itunes:explicit>
<itunes:duration>99:99:99</itunes:duration>
<itunes:keywords>key, words</itunes:keywords>
</item>
END;
$result = #simplexml_load_string($str);
$items = $result->xpath("//item");
foreach ($items as $item) {
echo $item->title . "\n";
echo $item->pubDate . "\n";
echo $item->children()->{'itunes:duration'} . "\n";
}
Output:
TITLE
YESTERDAY
99:99:99
Here goes my alternative solution if Dekel's dont work for someone.
Using method getNamespaces
$result = simplexml_load_file("http://blablabla.com/feed.xml");
$items = $result->xpath("//item");
foreach ($items as $item)
{
$itunesSpace = $item->getNameSpaces(true);
$nodes = $item->children($itunesSpace['itunes']);
//TEST
echo $nodes->subtitle
//99:99:99
echo $nodes->duration
//If you want the image Href
$imageAux = $nodes->image->attributes();
//yoyoyoyo.jpg
echo $imageAux['href'];
}

how can i load a xml, and save it with php?

hey guys I'm trying to parse a xml(feeds) and save it with php?(I'm halfway),.i already achieve to load the xml and display just the data i need to display,.now how can i save the xml?
this is my code :
<?php
$html = "";
$url = "http://www.conciencia.net/rss.aspx";
$xml = simplexml_load_file($url);
$channel_title = $xml->channel->title;
$channel_link = $xml->channel->link;
$managingEditor = $xml->channel->managingEditor;
$channel_description = $xml->channel->description;
$lbd = $xml->channel->lastBuildDate;
$html .= "<br/>$channel_title";
$html .= "<br/>$channel_link";
$html .= "<br/>$managingEditor";
$html .= "<br/>$lbd";
$html .= "<br/>$channel_description<br/>";
for($i = 0; $i < 7; $i++){
$pubDate = $xml->channel->item[$i]->pubDate;
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$guid = $xml->channel->item[$i]->guid;
$author = $xml->channel->item[$i]->author;
$description = $xml->channel->item[$i]->description;
$url0 = $xml->channel->item[$i]->enclosure->attributes()->url;
for($ii = 0; $ii < 2; $ii++){
$url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url;
}
$html .= "<br/>$pubDate";
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "<br/>$guid";
$html .= "<br/>$author";
$html .= "<br/>$description";
$html .= "<br/>$url0";
$html .= "<br/>$url";
$html .= "<br/>$link<br/>";
}
echo $html;
?>
that code is working good, loading the tags from the xml i just want to display,
what i want to do after that is create a xml(as is below structured) and save the data.
how can i achieve that ?
myxml.xml
<channel>
<title>$channel_title</title>
<link>$channel_link</link>
<managingEditor>$managingEditor</managingEditor>
<channel_description>$channel_description</channel_description>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
<item>
<title>$title</title>
<guid>$guid</guid>
<author>$author</author>
<description>$description</description>
<enclosure0>$url0</enclosure0>
<enclosure1>$url</enclosure>
</item>
</channel>
Well you can simly use normal string concatenation to write the xml file(alternatively you could spend time doing it the hard way with an xml object, which some might consider more proper).
Next you open a file with fopen() in write mode. if the file doesn't exist, it will be created. make sure you have permission to write to that directory, or the file will not be written(and if error reporting is strict, a fatal error will be thrown, else just a warning). second, you use fwrite() to write to the file.for more information, read the manual on filesystem functions
$file = '/path/to/mynewxmlfile.xml';
$data =
'
<?xml version="1.0"?>
<channel>
<title>'.$channel_title.'</title>
<link>'.$channel_link.'</link>
<managingEditor>'.'$managingEditor.</managingEditor>
<channel_description>'.$channel_description.'</channel_description>
';
for($i = 0; $i < 7; $i++){
$pubDate = $xml->channel->item[$i]->pubDate;
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$guid = $xml->channel->item[$i]->guid;
$author = $xml->channel->item[$i]->author;
$description = $xml->channel->item[$i]->description;
$url0 = $xml->channel->item[$i]->enclosure->attributes()->url;
for($ii = 0; $ii < 2; $ii++){
$url = $xml->channel->item[$i]->enclosure[$ii]->attributes()->url;
}
$data .=
'
<item>
<title>'.$title.'</title>
<guid>'.$guid.'</guid>
<author>'.$author.'</author>
<description>'.$description.'</description>
<enclosure0>'.$url0.'</enclosure0>
<enclosure1>'.$url.'</enclosure1>
</item>
';
}
$data .= '</channel>';
fwrite(fopen($file,'w'),$data);
Alternatively, you could use simplexml_load_string() to load the string as xml, then use asXml() to write it to disk, like so:
$data = simplexml_load_string($data);
$data->asXml($file);
Read the docs http://php.net/manual/en/simplexmlelement.asxml.php
$xml->asXml('rss.xml');
Edit
and if you want to create a new xml there is an answer https://stackoverflow.com/a/143192/1742977

Generate files from xml for each node

I would like to generate files from xml, I try this but it not working:
<?php
$file0 = "fle0.php";
$file1 = "fle1.php";
$file2 = "fle2.php";
$file3 = "fle3.php";
$file4 = "fle4.php";
$file5 = "fle5.php";
$file6 = "fle6.php";
$file7 = "fle7.php";
$file8 = "fle8.php";
$file9 = "fle9.php";
$html = "";
$url = "http://website.com/file.xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$link = $xml->books[$i]->link;
$title = $xml->books[$i]->title;
$html .= "$title";
file_put_contents($file[$i], $html);
}
?>
Can I use file_put_contents in for loop?
Here is the XML file:
<response>
<books>
<link>http:/www.website.com/linktobook/1.html</link>
<title>Book Title 1</title>
<image>http:/www.website.com/linktobook/1.jpg</image>
</books>
<books>
<link>http:/www.website.com/linktobook/2.html</link>
<title>Book Title 2</title>
<image>http:/www.website.com/linktobook/2.jpg</image>
</books>
<books>
<link>http:/www.website.com/linktobook/3.html</link>
<title>Book Title 3</title>
<image>http:/www.website.com/linktobook/3.jpg</image>
</books>
</response>
It is working without loop when I use $xml->books[0]->link; and $xml->books[1]->link; etc.
Thanks to: Jack
The answer is: change $file[$i] to ${"file$i"}

Searching XML tags with regex - PHP XPatch

I have a XML document:
<product>
<item>
<item00>
<name>DVD</name>
</item00>
</item>
</product>
<product>
<item>
<item11>
<name>CD</name>
</item11>
</item>
</product>
And I would like to show the names of these products, but there are products with item as "item00" and "item11".
I tried adding the path regular expressions in XPath, but without success.
There is a possibility I display the name of these products (DVD and CD) using XPath?
<?php
$xml = 'file.xml';
$content = '';
$f = fopen($xml, 'r');
while($data = fread($f, filesize($xml))) {
$content.= $data;
}
fclose($f);
preg_match_all('/\<product\>(.*?)\<\/product\>/s', $content, $product);
$product = $product[1];
$doc = new SimpleXMLElement($content);
for($i = 0; $i <= count($product) - 1; $i++) {
// So far, no problems. Seriously.
// The issue starts here.
$query = $doc->xpath('/product/item/???');
foreach($query as $item) {
echo $item->name . '<br>';
}
}
?>
Where "???" is the problem with "item00" and "item11".
If anyone knows and can help me, I'll be very grateful!
Here is the total working code
<?php
$xml = 'file.xml';
$content = '';
$f = fopen($xml, 'r');
while($data = fread($f, filesize($xml))) {
$content.= $data;
}
fclose($f);
$content = "<root>$conten</root>";
$doc = new SimpleXmlElement($content);
$query = $doc->xpath('//item/child::*');
foreach($query as $item) {
echo $item->name . '<br>';
}
i dont think you can use regex in that context, that's the very reason to use attributes
<item num="00">
however check this, i believe it is what you are looking for
those 00 11 things really should be attributes

PHP access XML node element

I am trying to edit some XML with PHP. Currently the XML looking something like:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Main Title</title>
<link>http://exmaple.com</link>
<description> blahblahblah </description>
<language>en</language>
<item>
<title>Tite1</title>
<link>http://www.example.com (THIS IS WHAT I WANT)</link>
<description>blah blah blah</description>
</item>
.
.
.
</channel>
</rss>
I've tried to access the 2nd level link but my code only changes the first Link node value. Here is the code:
$xml->load('http://www.google.com/doodles/doodles.xml');
$element = $xml->getElementsByTagName('channel')->item(0);
$secondlvl = $element->getElementsByTagName('item')->item(0);
$2ndlevellinknode = $element->getElementsByTagName('link')->item(0);
$2ndlevellinknode->nodeValue = $newvalue;
Any suggestions? Also is it possible to use this line of code in a for loop like this
for ($i = 0; $i <= 20; $i++) {
$element = $xml->getElementsByTagName('channel')->item(0);
$secondlvl = $element->getElementsByTagName('item')->item(0);
$2ndlevellinknode = $element->getElementsByTagName('link')->item($i);
$2ndlevellinknode->nodeValue = $newvalue;
}
this should give you an idea.
$f = simplexml_load_file('test.xml');
print $f->channel->title . "\n";
print $f->channel->link . "\n";
print $f->channel->description . "\n";
foreach($f->channel->item as $item) {
print $item->title . "\n";
}

Categories