echo or include multiple xml files in php page - php

I wish to create a list of XML files in a php file.
I need all my xml files to be included in my php file. I have this working see below.
<?php
header('Content-type: application/xml');
echo file_get_contents("exam1.xml");
when I try this it works well, now I try more than one.
<?php
header('Content-type: application/xml');
echo file_get_contents("exam1.xml");
echo file_get_contents("exam2.xml");
it dose not read any of the files.

I would try passing these files back in a JSON object.
Create an array of files and then json_encode() that array.
<?php
$f1 = file_get_contents("exam1.xml");
$f2 = file_get_contents("exam2.xml");
$response = array('files' => array($f1, $f2));
echo json_encode($response);
?>
Now you will have a simple json object that you can process in the javascript and place 1 or many xml file(s) whereever you want to on the page using straight forward javascript

Related

how to get the content of another php file without execution? [duplicate]

How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php

Easiest Way to read a xls File with PHP

I got a xls file on a hosted Webserver and now I am trying to read it with PHP. It is not important to read the whole file. I just need a few cell information to work with them. So what is the best way to do that?
//Try PHP-ExcelReader
<?php
include 'excel_reader.php'; // include the class
$excel = new PhpExcelReader; // creates object instance of the class
$excel->read('excel_file.xls'); // reads and stores the excel file data
// Test to see the excel data stored in $sheets property
echo '<pre>';
var_export($excel->sheets);
echo '</pre>';
or
echo '<pre>';
print_r($excel->sheets);
echo '</pre>';

Read a .php file using php

How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php

Parse a PHP file as an XML file?

I have been working on a project that displays data in an XML file. (It's kind of like an API). I know how to parse XML with PHP, and how to make an XML file in PHP, but they don't work together. :)
Basically, I have two files: parse.php and xml.php.
xml.php grabs info from a MySQL database, and outputs it as XML.
parse.php loads and parses xml.php and outputs it as HTML.
If I run parse.php, it does not load xml.php. However, if I copy the outputted XML (from xml.php) and save it as a xml.xml file (and change the filename in parse.php to 'xml.xml') it works. I'd really appreciate any help.
Content of parse.php:
<?php
$doc = "xml.php";
$doc = #simplexml_load_file($doc) or die("Server Error: Recipe not found!");
$title = $doc->title;
echo $title
?>
Content of xml.php:
<?php
header("Content-type: text/xml");
$dbc = mysql... //gets data from database
echo "<!DOCTYPE..."; //xml stuff here
echo "<title>" . $dataFromMySQL . "</title>";
?>
The database connection works, and the DOCTYPE in the XML is ok, so that's not the problem.
Again, I only get the problem when I generate XML dynamically using PHP. If it's a .XML file, it works fine.
Can anyone help me?
Thanks.
simplexml_load_file will try to actually load the php contents of the xml.php file. It will not run that file first. You need to do some rewriting or use this ugly solution:
ob_start();
include 'xml.php';
$xml = ob_get_clean();
$doc = simplexml_load_string($xml);
//...
NOTE: I like #lonesomeday's proposed solution better, it will just require more rewriting.
#simplexml_load_file($doc);
That is where your problem is. This does not execute xml.php, but attempts to parse that file -- the PHP code that you've written -- as XML. Obviously (since it isn't XML) this won't work.
You have to find a way of getting the output from executing xml.php into parse.php.
The easy way to do this would be to change all your echo calls into $xml .= calls, and simply include xml.php into parse.php.
// xml.php
$xml = '';
$xml .= "<!DOCTYPE..."; //xml stuff here
$output .= "<title>" . $dataFromMySQL . "</title>";
// parse.php
include('xml.php');
simplexml_load_string($xml);
Note that your problem here shows the foolishness of using the error suppression operator #. If you hadn't used it, PHP would have shown you various errors which would have helped you to realise what the problem was.
Addendum: it occurs to me that the best way actually is to forget about the pointless XML step along the way and just convert the database output into HTML.
If you want to do this without rewriting xml.php, you can get PHP to process the file by accessing via url:
$doc = file_get_contents("http://localhost/xml.php");
You're literally loading the local file. Unless you evaluate it, the code doesn't run, so you'll just get the code itself.
You could use CURL to download xml.php over HTTP, or you could make the XML-generation component of the xml.php a callable function which you simply include and execute.
parse.php:
<?php
include('xml.inc');
$doc = #simplexml_load_string(xml_function()) or die("Error");
echo $doc->title;
xml.php:
<?php
include('xml.inc');
header("Content-type: text/xml");
echo xml_function();
xml.inc:
<?php
function xml_function() {
$dbc = mysql... //gets data from database
$xml = "<!DOCTYPE..."; //xml stuff here
$xml .= "<title>" . $dataFromMySQL . "</title>";
return $xml;
}
But... even that seems silly, honestly, when you could have both output methods connect to the same data and skip a generation/parse step. Simply output HTML/XML conditionally.

How do I display (not execute) php file properly?

I have the following code:
<?
$serverurl = $_SERVER["DOCUMENT_ROOT"];
$file = $serverurl.'/demo/sample_php.php';
$newfile = $serverurl.'/demo/sample_php.txt';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
$homepage = file_get_contents($serverurl.'/demo/sample_php.txt');
?>
<pre class="code">
<code class="php boc-html-script">
<? echo htmlentities($homepage, ENT_QUOTES); ?>
</code>
</pre>
<? unlink($newfile); ?>
This basically copies a *.php file to a *.txt file, displays the contents, then deletes it. However, I don't want to create a visible file, as the application is designed to display a list of files, then display the contents of the file. Having a file appear with a .txt extension would be confusing.
I realize I could create a folder that is hidden, and do all my converting there, but I am thinking there must be a more efficient way to display the contents of a php file.
I did some experimenting with tmpfile(), but I couldn't get the contents of the php file to write to it.
Any ideas?
There is no reason to do the file copy. file_get_contents() returns the contents of the file as a string, which is all you need. It will not parse and execute the PHP code as include()/require() would.
Just retrieve the contents of the PHP file into the $homepage variable and echo it out as you have done with the temporary text file.
<?php
// get the PHP file directly.
$homepage = file_get_contents($serverurl.'/demo/sample_php.php');
?>
<pre class="code">
<code class="php boc-html-script">
<?php echo htmlentities($homepage, ENT_QUOTES); ?>
</code>
</pre>
After suggestions in the comments to print with highlighting, you can do it more easily with highlight_file():
highlight_file($serverurl.'/demo/sample_php.php');

Categories