echo'ing XML in PHP - php

why wont the following echo out the strings?
<?php
header("Content=type: text/xml");
echo '<?xml version="1.0" ?>';
echo '<strains>';
echo '<strainName>';
echo '</strainName>';
echo '</strains>';
?>
Basically I am experimenting with php and xml and noticed that when i create this script and run it, nothing is outputed to the screen?

The declaration of MIME type is incorrect. It should be:
header('Content-type: text/xml');

Works fine for me.
Open your php.ini and set the option
short_open_tag = Off
After that, restart your server.

Related

Reading an XML File in PHP

I originally had a php file pulling in data from another php file. However, I have to change the second php file to an xml file. My question is, how do I read the string between certain tags in the php file using SimpleXML. For Example, here is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<XMLExample>
<Name>ExampleName</Name>
<Info>ExampleInfo</Info>
<KMLFile>ExampleKML</KMLFile>
</XMLExample>
I previously had this in my php file:
if(strlen($KMLFile)>0){
#echo "<a id=\"links\" href=\"/$place/Area/$KMLFile\">KML File</a> \n";
echo "<a id=\"links\" href=\"/media/$place/markup/xml/$KMLFile\">KML File</a> \n";
}
What I can't figure out is how to change the php code to read the data from the XML file. Any help would be greatly appreciated! Thanks!
Use this code. note.xml is your .xml file.
<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
echo $xml->Name.'<br>';
echo $xml->Info.'<br>';
echo $xml->KMLFile;
?>

Read php source from another php script

I have two files reader.php and somesource.php. Both in the same folder.
somesource.php content inside the php tag.
echo "hello World";
reader.php contents
$fp = fopen('somesource.php','r') or die($php_errormsg);
$string = fread($fp,filesize('somesource.php'));
echo $string."<br>";
I am expecting to output
echo "hello World";
But I am seeing a blank page. I even tried. curl and file_get_contents. Both with the same output. If I write anything outside the php tag wil be echoed as normal. anything inside the php tag is skipped.
Please Help
use
echo htmlspecialchars($string)."<br>";
Depending on your server setup it may not read it without <?php echo $stuff; ?> in there. <? aka short tags can break a lot of stuff in my experience.

XML not working with php

I made a code in php. I want a XML output in browser but it shows error. Actually it works fine in locally. But when i hosted it shows error as "XML Parsing Error: junk after document element".
<?php
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
echo '<group>';
echo '<family>';
echo '<person>';
echo 'first name';
echo '</person>';
echo '</family>';
echo '</group>';
?>
plese help me.
You have a white-space before your <\?php so before <\?xml
It is causing the error.
Put a die(); after echo '</group>';.
If that clears up your error, then something is being output after the code you posted.
Alternatively you could prepare your XML like:
<?php header("Content-type: text/xml"); ?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<group>
<family>
<person>first name</person>
</family>
</group>
Your script itself works fine in my local environment.
The problem probably comes from somewhere else.
You may have something outputed after your script.
And it is not a white-space before php cause otherwise you would have an error similar to :
XML declaration allowed only at the start of the document

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.

Include whole content of a file and echo it

I need to echo entire content of included file. I have tried the below:
echo "<?php include ('http://www.example.com/script.php'); ?>";
echo "include (\"http://www.example.com/script.php\");";
But neither works? Does PHP support this?
Just do:
include("http://www.mysite.com/script.php");
Or:
echo file_get_contents("http://www.mysite.com/script.php");
Notes:
This may slow down your page due to network latency or if the other server is slow.
This requires allow_url_fopen to be on for your PHP installation. Some hosts turn it off.
This will not give you the PHP code, it'll give you the HTML/text output.
Shortest way is:
readfile('http://www.mysite.com/script.php');
That will directly output the file.
Echo prints something to the output buffer - it's not parsed by PHP. If you want to include something, just do it
include ('http://www.mysite.com/script.php');
You don't need to print out PHP source code, when you're writing PHP source code.
Not really sure what you're asking, but you can't really include something via http and expect to see code, since the server will parse the file.
If "script.php" is a local file, you could try something like:
$file = file_get_contents('script.php');
echo $file;
This may not be the exact answer to your question, but why don't you just close the echo statement, insert your include statement, and then add a new echo statement?
<?php
echo 'The brown cow';
include './script.php';
echo 'jumped over the fence.';
?>
Matt is correct with readfile() but it also may be helpful for someone to look into the PHP file handling functions
manual entry for fpassthru
<?php
$f = fopen($filepath, 'r');
fpassthru($f);
fclose($f);
?>

Categories