Copying Resulting Output to Another File in PHP - php

I am trying to take a two-tiered foreach which outputs an xml - and copy only the resulting output of that into another file.
I am so confused that I don't even know if to ask "how to copy output to other file" or "how to convert foreach into string" - because writing a string over is no problem.
So far I have
// clear previous contents of tutorials.xml
$myFile = "tutorials.xml";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "";
fwrite($fh, $stringData);
// here begins the string I want to write to the other file
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<albums>
EOF;
foreach($albumInfo as $album) {
echo <<<EOF
<album>
<id>{$album->id}</id>
<title>{$album->title}</title>
<videos>
EOF;
}
foreach($videos as $video) // loop through our videos
{
$minutes = floor($video->duration/60);
$secondsleft = $video->duration%60;
if($secondsleft<10)
$secondsleft = "0" . $secondsleft;
echo <<<EOF
<video>
<id>{$video->id}</id>
<title>
<description>{$video->description}</description>
<duration>{$video->duration}</duration>
</video>
EOF;
}
echo <<<EOF
</album>
EOF;
?>
<?php echo '</albums>' ?>
// here ends the string I want to write to the other file
// the script below just takes the raw php and copies it over - I need the output.
<?php
copy('tutorials-job.php', 'tutorials.xml');
?>
Thank you for your help!

You already show that you know how to output content to a file with the first few lines of this code. You can either load your output into an output buffer and then write the contents of the buffer to the file, or instead of echoing everything, put that data into a string and just write that string to a file.
That being said, if you are not stuck on XML format, you might consider using something like JSON to where you can easily convert between your array/object representation to your serialized representation with a single line of code.

replace echo with an assignment to a variable then write that to the file
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<albums>
EOF;
becomes
$str='
<?xml version="1.0" encoding="UTF-8"?>
<albums>
';
latter use
$str .='
to add to the string

Related

Php delete element in xml

I'm trying to use php to delete an xml element but it doesn't work. I tried some different code but no one works. I would also like to use cookies to get element in the future. Can you suggest me what I have to do ? I'm not expert and for this I'm in difficulty.
Here the code:
<?php
$dom = new DOMDocument();
$dom->load("Dati.xml");
$matchingElements = $dom->getElementsByTagName("Matematica");
$totalMatches = $matchingElements->length;
$elementsToDelete = array();
$elementsToDelete[] = $matchingElements->item(0);
foreach ( $elementsToDelete as $elementToDelete ) {
$elementToDelete->parentNode->removeChild($elementToDelete);
}
$dom->save($xmlFileToLoad);
echo "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";
echo "Puoi chiudere questa pagina";
?>
Here the xml:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<Informatica>
<nome>aaaa</nome>
<classe>3C</classe>
<titolo>Informatica</titolo>
<materia>Informatica</materia>
<ISBN>123456789101112</ISBN>
<prezzo>12</prezzo>
<autori>tizio</autori>
<contatto>nanni-lombardo1#hotmail.it</contatto>
<codice>123456</codice>
</Informatica>
<Matematica>
<nome>bbb</nome>
<classe>3C</classe>
<titolo>math</titolo>
<materia>Matematica</materia>
<ISBN>123456789101112</ISBN>
<prezzo>12</prezzo>
<autori>tizio</autori>
<contatto>nanni-lombardo1#hotmail.it</contatto>
<codice>123456</codice>
</Matematica>
</document>
Please be sure to :
Remove the space before your XML start tag
Define your $xmlFileToLoad variable
Make your destination XML writable (see Chmod & permissions)
My test file works fine : https://eu.andredasilva.fr/testandre/test.php (source code : https://eu.andredasilva.fr/testandre/test.php.source)
Base XML : https://eu.andredasilva.fr/testandre/Dati.xml
Result XML : https://eu.andredasilva.fr/testandre/test.xml

PHP - Delete last line from xml

I have this XML file called help_cat.xml
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
</ROOT>
I want to do something like delete </ROOT> from the XML add new content and give </ROOT> back at the end of the XML file.
But I have the problem with the first step. Delete </ROOT>.
I tried
<?php
$file = "help_cat.xml";
$lines = file($file);
echo sizeof($lines)."<BR />";
$last = sizeof($lines)-1;
echo $lines[$last]."<BR />";
unset($lines[$last]);
?>
The script wrote well that there are 3 rows in the file, but can't delete last row. Could someone help me, please?

php- How to send $_POST data to xml?

I have a php file that contains:
<?php
$no = $_POST['no'];
$url = "http://domain.tld/answer.xml";
?>
The $url stores a URL that links to an xml file and that xml files is like:
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<Call> </Call>
</vxml>
I want to pass the $_POST['no'] to my xml file. Suppose if $_POST['no'] is 9999988888, then the tag in xml should be like:
<Call>9999988888</Call>
Can anyone tell how to do this? :)
that xml file is on my own server.
you could use simplexml, as:
$no = $_POST['no'];
$xml = simplexml_load_file('your_xml_file.xml');
$xml->Call = $no;
//save/update your xml
$xml->asXML('your_xml_file.xml');
You need to generate the xml using PHP.
First of all, rename your .xml to .php.
Then, start it using
<?php
header('Content-type: text/xml');
?>
When you reach your call tag, change it to :
<Call><?php echo intval($_GET['no']); ?></Call>
Note: I've assumed your no is a number. If it's not, remove intval. It's nice to have it as it will protect you from injections.
At last, call your XML file using :
$url = "http://domain.tld/answer.php?no=8877454";
Your XML should have some keywords, i.e.
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<Call>{{CALL}}</Call>
</vxml>
In your PHP File, just fetch this XML & use str_replace to replace those keywords,
<?php
$no = $_POST['no'];
$url = "http://domain.tld/answer.xml";
$xml = file_get_contents($url);
$newXML = str_replace("{{CALL}}", $no, $xml);
header('Content-type: text/xml');
echo $newXML;
?>

Parsing xml file with .php extension and php header

I have a slight problem. I need to parse a file and populate a web banner with the results. Problem is, the file is called : "_banner_products.php" and it's contents are as follows:
<?php header('Content-Type:text/xml'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
.....
.....
</firstLayer>
</carouselle>
How can I loop through this file to group all the "firstLayer" children into one and so on..
If I just use:
$file = fopen("_banner_products.php", "r");
while (!feof($file)){
$line = fgets($file);
}
simplexml_load_file throws this-
"_banner_products.php:1: parser error : Start tag expected, '<' "
Then I only get the contents of the <...> tags meaning there is no way for me to differentiate if I am out of the scope already.
Thanks for anyone responding. If anything is unclear I´ll try to explain more.
EDIT.
Thank you for the solution, indeed using the full URL worked:
simplexml_load_file("http://localhost/MySite/_banner_products.php");
You are having issue because simplexml_load_file is treating your file like a local xml file .. what you need to do is add the full URL
Example
simplexml_load_file("http://localhost/web/_banner_products.php");
Use Case getting layerName for example
_banner_products.php
<?php
header ( 'Content-Type:text/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
</carouselle>
view details
$xml = simplexml_load_file("http://localhost/lab/stockoverflow/_banner_products.php");
echo "<pre>" ;
foreach($xml as $key => $element)
{
echo $element->layerName , PHP_EOL ;
}
The most obvious way to do this is to strip out the first line, and add the XML declaration back in with your code.
You could also parse the file with PHP, using eval(), but be very sure about what you are parsing, as this could be a very large security hole.

XML parsing using PHP

I have a .out file which has xml content like this.
<header stub>
<article type="audio">
<addedDate>2010-03-11 05:11:57</addedDate>
<thumbnail>http://fgsdfff/4588/thumbnail_9.jpg</thumbnail>
<asset="blarga.mp3" addedDate="2009-01-07 01:48:37">
<size>3289048</size>
<duration>206000</duration>
<mime_type>audio/mpeg</mime_type>
</asset>
</article>
</footer stub>
I have to add <?xml version="1.0"?> and </xml> at the start of the xml and at the end respectively.
I have to replace <header stub> and </footer stub> with <channel> and </channel> tags respectively.
You might have noticed the XML is not well formed in the <asset> tag. It has to be like <asset url="blarga.mp3" addedDate="2009-01-07 01:48:37">. How do I add the url attribute?
In the thumbnail tag, i have to remove _9 from the jpg's name.
And finally, i have to convert the .out to .xml file.
Please help me with these
$content = file_get_content OR mysql_query;//"YOUR XML CONTENT FROM FILE OR MYSQL";
$content = "<?xml version="1.0"?>" . $content . "</xml>";
$content = str_replace("<header stub>", "<channel>", $content);
$content = str_replace("</footer stub>", "</channel>", $content);
$content = str_replace("<asset=", "<asset url=", $content);
$content = str_replace("thumbnail_9.jpg", "thumbnail.jpg", $content);
You can use DOMDocument class.
An example of its usage can be found here

Categories