creating XML output in PHP - php

I am trying to create an XML file output in PHP for a remote phone book on an IP Phone, here is the code i have:
<?php
$conn=mysql_connect("localhost","user","********");
mysql_select_db("db_name",$conn);
header("Content-Type: text/xml");
header("Connection: close");
header("Expires: -1");
?>
<YealinkIPPhoneDirectory>
<?php
$output='<YealinkIPPhoneDirectory>\n';
$sql="SELECT * from contacts ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs)) {
$output .= "<DirectoryEntry>\n";
$output .= "<Name>Mobile:</Name>\n";
$output .= "<Telephone>" . $result["mobile"] . "</Telephone>\n";
$output .= "</DirectoryEntry>\n";
}
$output='</YealinkIPPhoneDirectory>\n';
echo '$output';
?>
but i get this error message:
This page contains the following errors:
error on line 3 at column 8: Extra content at the end of the document
Below is a rendering of the page up to the first error.
$output

<?php
$conn=mysql_connect("localhost","user","********");
mysql_select_db("db_name",$conn);
header("Content-Type: text/xml");
header("Connection: close");
header("Expires: -1");
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$output .= '<YealinkIPPhoneDirectory>\n';
$sql="SELECT * from contacts ";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs)) {
$output .= "<DirectoryEntry>\n";
$output .= "<Name>Mobile:</Name>\n";
$output .= "<Telephone>" . $result["mobile"] . "</Telephone>\n";
$output .= "</DirectoryEntry>\n";
}
$output.='</YealinkIPPhoneDirectory>';
echo $output;
?>

You need to remove this line:
echo '$output';
and replace it with this:
echo $output;
Here is a sample:
https://eval.in/96740
Note how singlequoted values are literally echoed, so you literally see $output as your output.

Related

using <![CDATA in xml response

I have looked for an answer to this question but I have been unable to find an answer that works for my application.
I need to give a response from a sql query in xml format but the response is a url so i will need to use cdata. Here is my code.
if ($_POST['action']=="getSermon")
{
//echo"You got it";
$SQL = "SELECT * FROM sermons";
$allsermon = mysql_query($SQL);
$sermonrow = mysql_fetch_assoc($allsermon);
header('Content-type: text/xml; charset=utf-8');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
$xml = '<?xml version="1.0" encoding="utf-8"?>';
echo $xml;
echo "<SERMON_INFO>";
do {
echo "<SERMON>";
echo "<ID>" . $sermonrow['id'] . "</ID>";
echo "<NAME>" . $sermonrow['Name'] . "</NAME>";
echo "<URL>".<![CDATA[$sermonrow['url']]>."</NAME>";
//echo "<URL>". $sermonrow['url'] . "</URL>";
echo "</SERMON>";
mysql_free_result($result);
} while ($sermonrow = mysql_fetch_assoc($allsermon));
mysql_free_result($allsermon);
echo "</SERMON_INFO>";
mysql_close($db);
}
when i do this i get an error 500 and the page will not load.
The correct syntax is
echo "<URL><![CDATA[".$sermonrow['url']."]]></URL>";
But I'd rather you use an XML library like DOMDocument or SimpleXML to build your document, more complex, but you'll be less prone to simple errors like mixing up a closing tag name (see </NAME>).

Export MySQL Data to MS Excel is working well in local but not on server

<title>Orders Export</title>
<body>
<?php
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
$con = mysqli_connect('localhost','suresafe_admin','iwant$100','suresafe_suresafety');
$query = "select * from `orders_approval` where `approve`='1' and `company_name`='GAVL-F111'";
$result = mysqli_query($con,$query);
$output = '';
if(mysqli_num_rows($result) > 0)
{
$output .= '?>
<table class="table" bordered="1">
<tr>
<th>order_no</th>
<th>order_date</th>
<th>order_name</th>
<th>total_amount</th>
</tr><?php
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["order_no"].'</td>
<td>'.$row["order_date"].'</td>
<td>'.$row["order_name"].'</td>
<td>'.$row["total_amount"].'</td>
</tr>
';
}
$output .= '</table>';
}?>
</body>
</html>
The code is simple. Working well in local. But when I use this in my website on server. It shows only table with data. It don't export that data in Excel.
order_no order_date order_name total_amount
100000705 2017-05-07 MR. PRADEEP Y 113500
100000708 2017-05-11 MR. A SRINIVASA RAO 5448
100000725 2017-05-30 MR. A SRINIVASA RAO 77180
Here is the result I can see when I click on export link.
In local server, it is easily exported.
If you had configured your server properly, you would be seeing warnings. You cannot send a header after data has already been output. In addition, the data you're sending is not application/xls (which is not a valid MIME type) it's text/html. You're also outputting "?>" and "<?php" into your HTML, which will not work too well.
However, I would suggest going with CSV for the data you're outputting:
<?php
$con = mysqli_connect("localhost", "suresafe_admin", "iwant$100", "suresafe_suresafety");
$query = "SELECT order_no, order_date, order_name, total_amount from orders_approval WHERE approve = 1 AND company_name = 'GAVL-F111'";
$result = mysqli_query($con, $query);
$output = "";
if(mysqli_num_rows($result) > 0) {
$output .= "order_no,order_date,order_name,total_amount\n";
while($row = mysqli_fetch_assoc($result)) {
$output .= "$row[order_no],$row[order_date],$row[order_name],$row[total_amount]\n";
}
}
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=download.csv");
echo $output;

How to remove space from start of a php file?

Have a PHP file which is making RSS feeds it is giving error because of the space appending before the output XML file. I have checked the code and the variable containing the variable $s which is making XML file does not have any white space before it.
<?php
header("Content-Type: text/xml; encoding=UTF-8");
$s = "<?xml version='1.0' encoding='UTF-8'?><rss version='2.0'><channel>";
echo ltrim($s);
foreach($this->results as $row)
{
$title=$row->name;
$description=$row->description;
echo "<item><title>$title</title><link>$link</link><description>$description</description></item>";
}
echo "</channel></rss>";
?>
Make sure no white space before <?php
<?php
header("Content-Type: text/xml; encoding=UTF-8");
$s = "<?xml version='1.0' encoding='UTF-8'?><rss version='2.0'><channel>";
echo ltrim($s);
foreach($this->results as $row)
{
$title=$row->name;
$description=$row->description;
echo "<item><title>$title</title><link>$link</link><description>$description</description></item>";
}
echo "</channel></rss>";
?>

My prog do not fetching Marathi database values in exiting PDF template

Can anybody help me why my code is not fetching Marahti database values in PDF.
Here is my Code:
<?php
GLOBAL $html;
mb_internal_encoding("UTF-8"); header('Content-Type: text/html; charset=utf-8');
ini_set('display_errors', true);
$con=mysql_connect('localhost','root') or die('Error');
mysql_select_db('DemoMarathi');
$result = mysql_query("SET NAMES utf8")or die('error'.mysql_error());
$cmd = "select * from Demo_hindi";
$result = mysql_query($cmd);
while($row = mysql_fetch_assoc($result))
{
$html .= '<td>'.$row['DemoMarathi'].'</td><td>' . $row['DemoValues']. '</td>';
$pdf->Text(105, 73,"hi".$myrow['DemoMarathi']);
$pdf->Text(105, 73,"hi".$myrow['DemoValues']);
$pdf->Write(75,"".$myrow['DemoMarathi']);
$pdf->Write(90,"".$myrow['DemoValues']);
}
include('MPDF FULL_PACKAGE/mpdf60/mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML($html.$myrow['DemoMarathi']);
$mpdf->WriteHTML($html.$myrow['DemoValues']);
$mpdf->Output();
exit;
?>
PDF OUTPUT FILE

XML Parsing Error: XML or text declaration not at start of entity

I have this error in my rss.php
XML Parsing Error: XML or text declaration not at start of entity
Location: http://blah.com/blah/blah/site/rss.php
Line Number 1, Column 3:
and this is shown underneath the error
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><item><title>xzfbcbcxv 123</title><description><p>Description for the rss feed</p></description></item></channel></rss>
The
----------------^
is show between the left side of the page and the ?xml line.
In my rss.php I have
<?php header("Content-type: text/xml"); ?>
<?php include("includes/database.php");?>
<?php global $_TWITTER_TABLE, $_HTTP_ADDRESS, $_NEWS_TABLE; ?>
<?php $str = '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<?php $str .= '<rss version="2.0">';?>
<?php
$str.='<channel>';
$sql = "SELECT * FROM $_NEWS_TABLE LIMIT 5";
$result = mysql_query($sql) or die ($sql."".mysql_error());
while($row = mysql_fetch_object($result)){
$str.= '<item>';
$str.= '<title>'.$row->title. '</title>';
$str.= '<description>'.$row->content. '</description>';
$str.= '</item>';
}
$str.= '</channel>';
?>
<?php $str .= '</rss>';
echo $str;
?>
You have a lot of white space before the declaration. You need to remove it. This can be caused by having closing PHP tags followed by spaces or tabs or new line at the end of included files. You can prevent this by just not closing the PHP tags.
Second look, remove all the closing tags at the top of your document:
<?php // make sure that this is the first character in the file, no spaces before it
header("Content-type: text/xml");
include("includes/database.php");
global $_TWITTER_TABLE, $_HTTP_ADDRESS, $_NEWS_TABLE;
$str = '<?xml version="1.0" encoding="UTF-8"?>';
$str .= '<rss version="2.0">';
$str.='<channel>';
$sql = "SELECT * FROM $_NEWS_TABLE LIMIT 5";
$result = mysql_query($sql) or die ($sql."".mysql_error());
while($row = mysql_fetch_object($result)){
$str.= '<item>';
$str.= '<title>'.$row->title. '</title>';
$str.= '<description>'.$row->content. '</description>';
$str.= '</item>';
}
echo $str;
Also, just a note, the mysql_* functions are deprecated due to security problems. You may want to look at mysqli and PDO
I just remove the space from the top of wp-config file and its works for me...
This could be a problem with the XML parsing White-Space.
I'm also faced this problem, remove all unwanted line spaces in your code,i mean remove all unwanted line spaces before and after the php tags.
There can be more reasons but 95% this is the reason of blank space after or before php tags. <?php ?> .

Categories