i have the following code.
$stmt = $conn->prepare('SELECT sponsor_path FROM sponsors WHERE conference_id = ?');
$stmt->execute(array($cid));
$sponsorsImg = $stmt->fetchall (PDO::FETCH_OBJ);
$xml_output = "<?xml version=\"1.0\" ?>";
$xml_output .= "<sponsors>";
foreach ($sponsorsImg as $spImg){
$xml_output .= "<sponsor>\n";
$xml_output .= "<image>".$spImg->sponsor_path."</image>\n";
$xml_output .= "</sponsor>\n";
}
$xml_output .= "</sponsors>";
echo $xml_output;
Instead of printing the results in xml format i get only the $spImg->sponsor_path's content in plain text. Any ideas?
Most browsers will render markup that looks like XML. So it might be the case that the XML that you want is actually produced but not displayed correctly on the browser.
Try the following:
Set the content type before outputting:
header('Content-Type: text/xml');
echo $xml_output;
If the output looks the same, then right click on the page and select view source. You should see your XML markup as intended there.
Related
I'm trying to store a xml as string in a variable so that I can store it in my database.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";
When I echo it, it's not displayed at all as if my web brower reads it as html tag. It doesn't even look like $xml is storing those as texts. Now, I'm trying to do it with DOMDocument... not not quite successful yet. Any tips? :(
Edited my stupid += mistakes..
PHP uses a . as a concatenate operator, or a .= as a shortcut, not a + or +=.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";
I'm trying to create a rss feed that displays all my news.
So far the code seems to be half way there because if I view page source all the content is there but nothing is showing up on the actual page.
Here is my code rss.php
<?php
ini_set('display_errors', 1);
header("Content-type: text/xml");
include("config.php");
global $NEWS;
$str = '<?xml version="1.0" encoding="utf-8"?>';
$str.= '<rss version="2.0">';
$str.='<channel>';
$sql = "SELECT * FROM $NEWS";
$result = mysql_query($sql) or die ($sql."".mysql_error());
while($row = mysql_fetch_object($result)){
$str.= '<item>';
$str.=' <a href="'.getSEOLink(13).'&article='.$row->id.'">';
$str.= '<title>'.$row->title.'</title></a>';
$str.= '<description><![CDATA['.$row->content. ']]></description>';
$str.= '</item>';
}
$str .='</channel>';
$str .='</rss>';
echo $str;
?>
Why not make use of asXML of SimpleXMLElement ?
<?php
ini_set('display_errors', 1);
include("config.php");
global $NEWS;
$str='<channel>';
$sql = "SELECT * FROM $NEWS";
$result = mysql_query($sql) or die ($sql."".mysql_error());
while($row = mysql_fetch_object($result)){
$str.= '<item>';
$str.=' <a href="'.getSEOLink(13).'&article='.$row->id.'">';
$str.= '<title>'.$row->title.'</title></a>';
$str.= '<description><![CDATA['.$row->content. ']]></description>';
$str.= '</item>';
}
$str .='</channel>';
$xml=new SimpleXMLElement($str);
echo $xml->asXML();
You are apparently generating valid XML (thus your browser is not complaining) but not valid RSS (thus feed readers won't be able to read your stuff). For instance, <a> is not a valid RSS tag and definitively not a child of <item>.
Copy the generated XML (not the PHP source code) and validate it with your favourite RSS validator (e.g. W3C Feed Validation Service).
I have a problem with XML data generated by a PHP file.
here is my code:
$requestXmlBody .= "<PictureURL>";
//find black
while($row = mysql_fetch_object($ergebnis))
{
$posblack = strpos($row->image, $findBLACK);
if ($posblack !== false)
{
echo $row->image;
}
}
$requestXmlBody .= "</PictureURL>";
This code will generate the XML Code
<PictureURL></PictureURL>
but not the name I fetch from the database. The database query is working but my problem ist to have it inserted between the XML code.
Usually a variable is inserted like this
$requestXmlBody .= '<PictureURL>$variable</PictureURL>';
I just don't know how to wrap this around my database query.
Any help is very appreciated.
Something like this?
while($row = mysql_fetch_object($ergebnis)) {
$posblack = strpos($row->image, $findBLACK);
if ($posblack !== false) {
$requestXmlBody .= "<PictureURL>";
$requestXmlBody .= $row->image;
$requestXmlBody .= "</PictureURL>";
}
}
You are echo'ing $row->image instead of appending to $requestXmlBody
Change your code to:
$requestXmlBody .= $row->image;
I am trying to load in XML to populate a search box. The code I have works fine with a static xml file, but I need to load in a PHP file to generate a dynamic XML file based on data from a database.
I knwo the PHP file generates the XML as it loads in a browser and the page source shows the correct nodes etc, however when I reference the .php file in my JavaScript it fails to load, or show an error... Using the .xml file (which is a replica of the php output source) it loads fine.
I would like someone to check to see if I am encoding the XML correctly or missing something in my JavaScript... and advice will be greatly appreciated.
JS
var myArr = [];
$.ajax({
url: "people.php", // change to full path of file on server
type: "GET",
dataType: "xml",
success: parseXml,
complete: setupAC,
failure: function(data) {
alert("XML File could not be found");
}
});
function parseXml(xml){
$(xml).find("person").each(function(){
var thisItem = $(this).find('name').text();
myArr.push(thisItem);
alert(thisItem);
});
}
PHP
<?php
include '../../inc_global/connect.php';
$query = 'SELECT * FROM candidates';
$result = mysql_query($query, $link);
$xmlOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xmlOutput .= "<people>\n";
while ($line = mysql_fetch_assoc($result)) {
$xmlOutput .= "<person>\n";
$xmlOutput .= "<name>" . $line['name'] . "</name>\n";
$xmlOutput .= "</person>\n";
}
$xmlOutput .= "</people>\n";
echo $xmlOutput;
mysql_close($link);
?>
Try to put header right before outputting information:
....
header ("Content-Type:text/xml");
echo $xmlOutput;
....
Fullcode:
<?php
include '../../inc_global/connect.php';
$query = 'SELECT * FROM candidates';
$result = mysql_query($query, $link);
$xmlOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xmlOutput .= "<people>\n";
while ($line = mysql_fetch_assoc($result)) {
$xmlOutput .= "<person>\n";
$xmlOutput .= "<name>" . $line['name'] . "</name>\n";
$xmlOutput .= "</person>\n";
}
$xmlOutput .= "</people>\n";
header ("Content-Type:text/xml"); //<----------- xml header here
echo $xmlOutput;
mysql_close($link);
?>
Check what's happening in php pointing your browser to people.php.
Anyway I advice you to:
drop mysql_ for PDO
As mysql_ funcs are bad and will prevent from meeting chicks, while PDO helps;
use SimpleXML against raw XML
SimpleXML helps you structure your code without getting mad in chaining strings.
give proper HTTP header to your document
You are generating an XML document, let the browser be aware of that:
header ("Content-Type:text/xml");
echo $xmlOutput;
Can you make sure that the function parseXml(xml) doesn't need any parameter when it is called on success of ajax call. Please note that I haven't tried the code but just a guess is here.
Upon transfering my site from one server to another, some functions started behaving differently.
The problem lies in building my rss feed. My feed are composed of database posts containing html, and to show them correctly in the feed I then need to change the picture paths to full domain paths, instead of just server root. For this purpose I use simple html dom (http://simplehtmldom.sourceforge.net).
When outputting the varaible html in Content-type: appilication/rss+xml it will show "Object id #1". Should I choose to output it before setting content-type, I get the right html. (in this case < p >test< /p >)
How come I can't output the variable html inside content-type rss+xml?
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/config.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/functions/simple_html_dom.php');
$html = str_get_html("<p>test</p>");
foreach($html->find('img') as $e) {
$e->src = $siteconfig['full_domain'].$e->src;
}
header("Content-Type: application/rss+xml; charset=UTF-8");
$rssfeed = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$rssfeed .= "<rss version=\"2.0\">\n";
$rssfeed .= "<channel>\n";
$rssfeed .= "<title>Headline</title>\n";
$rssfeed .= "<language>da</language>\n";
$rssfeed .= "<item>\n\t";
$rssfeed .= "<description><![CDATA[".$html."]]></description>\n\t";
$rssfeed .= "</item>\n";
$rssfeed .= "</channel>\n";
$rssfeed .= "</rss>";
echo $rssfeed;
try this:
$rssfeed .= "<description><![CDATA[".$html->save()."]]></description>\n\t";
Looks really weird. Just try this, for sports: echo $rssfeed." ";
Try also removing everything below header, what's the output then?