Output in XML using PHP - php

header('content-type: application/xml');
echo '<'.'?xml version="1.0"?'.'>';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo '<root>';
$hierarchy=$tree->getArray();
recursiveBuild($hierarchy[0]);
echo '</root>';
I am printing this as output to get XML... can i get these contents in a File.xml instead of file.php
Instead of seeing the xml in a php file, i need this to be outputted in direct XML.

You could buffer the output with the Output Buffering Control and write the buffered data into a file with file_put_contents:
ob_start();
echo '<'.'?xml version="1.0"?'.'>';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo '<root>';
$hierarchy=$tree->getArray();
recursiveBuild($hierarchy[0]);
echo '</root>';
file_put_contents('file.xml', ob_get_contents());
ob_end_clean();

Related

How to redirect after fopen() in php

I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).

Can not get node value using simplexml in php

I have the following XML file
<?xml version="1.0" encoding="UTF-8"?>
<newsItem>
<contentSet>
<inlineXML contenttype="application/xhtml+xml">
<html xmlns="http://www.w3.org/1999/xhtml">
<div>
<h1>St. Augustine Gold and Copper Limited: Update on Recent Corporate Developments</h1>
</div>
</html>
</inlineXML>
</contentSet>
</newsItem>
I want to get the value in using with the following code with no problem
if (file_exists('example.newsml')) {
$xml = simplexml_load_file('example.newsml');
$html= (string) $xml->{'contentSet'}->{'inlineXML'}->{'html'}->{'div'}->{'h1'};
echo $html;
} else {
exit('Failed to open test.xml.');
}
I tried to get the html from the node but get empty result.
$content = (string) $xml->{'contentSet'}->{'inlineXML'}->{'html'};
echo $content;
Any suggestions?
Just save the node as XML
echo $xml->contentSet->inlineXML->html->saveXml();
Output: https://eval.in/205477
<html xmlns="http://www.w3.org/1999/xhtml">
<div>
<h1>St. Augustine Gold and Copper Limited: Update on Recent Corporate Developments</h1>
</div>
</html>

Php - fwrite bad characters

I'm having a problem with file_get_contents and fwrite.
To get a script to work I have to print content from an external URL into a html file.
I'm using this code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$url = 'http://www.vasttrafik.se/nasta-tur-fullskarm/?externalid=9021014005135000';
$content = file_get_contents($url);
echo $content; // Actually writes out correct
$myFile = "response.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $content); // Doesn't write out correct ???
fclose($fh);
?>
</body>
</html>
When I echo out the file_get_contents, the HTML shows up nicely (with the Swedish special characters: åäö)
However.. The file "response.php" shows bad characters instead of åäö.
Any ideas? Does the fwrite use another encoding?
Thanks!
UPDATE!
Solved with this:
$content = "\xEF\xBB\xBF";
$content .= utf8_encode(file_get_contents($url));
SOLVED!
I needed to ad a BOM (Byte Order Mark) AND utf8_encode.
Like this:
$content = "\xEF\xBB\xBF";
$content .= utf8_encode(file_get_contents($url));

Pass value to a php file to change the text inside the file then force download the same file

Download link:
Download php file
download.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Home</title>
</head>
<body>
<h1>PHP file</h1>
<?php
function get_text($text)
{
....
}
function get_time($time)
{
....
}
$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
$xml = simplexml_load_file($url);
if (count($xml))
{
foreach($xml->book as $book)
{
echo ....
}
}
?>
</body>
</html>
The download.php is a ready made API php script to provide webmasters upload to their FTP. Webmasters can be choose many options(e.g: download.php?words=2000&sort=popular&type=xml) from a form, then submit the form to get their custom API script.
This is the line that will replace the options after they submit the form.:
$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
This is the code to force download. But i don't know how to wrap whole page with $content = "";. I know how to wrap the HTML codes but how to wrap the PHP function and codes on the page?
header("Content-Disposition: attachment; filename="download.php");
print $content;
Not sure if that is what yo want; but you could create a second script that calls the first one, gets the output, and sends that with the mentioned headers:
<?php
$words = (int) $_GET['words'];
$sort = $_GET['sort'];
$url = sprintf("http://localhost/wherever/download.php?words=%d&sort=%s&type=xml", $words, $sort);
$content = file_get_contents($url);
header("Content-Disposition: attachment; filename="download.php");
print $content;
?>
Call that file "force_download.php" and let the users call Download php file instead.

PHP file_put_contents new line

I tried use file_put_contents output new page. but I meet some trouble in breaking new line.
<?php
$data ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n';
$data .='<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n';
$data .='<head>\r\n';
$data .='</head>\r\n';
$data .='<body>\r\n';
$data .='<p>put something here</p>\r\n';
$data .='</body>\r\n';
$data .='</html>\r\n';
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
I tried \n or \r\n, they all can not make a new line:
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n<head>\r\n</head>\r\n<body>\r\n<p>put something here</p>\r\n</body>\r\n</html>\r\n
Using \r or \n in single quotes carries it literally. use double quotes instead like "\r\n"
So one line might become:
$data .= "<head>\r\n";
or
$data .='<head>' . "\r\n";
You are using single-quoted character literals, which don't interpret escape sequences.
Either switch to double-quoted strings or, preferably, use heredoc syntax.
<?php
$data = <<<CONTENTS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
</head>
<body>
<p>put something here</p>
</body>
</html>
CONTENTS;
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
But really, why are you writing a hard-coded file? That's really strange.

Categories