how to echo an xml in php? - php

I have this function and I need to echo an XML but it doesnt work. what is the problem?
static function login_xml($ERROR_ID,$SESSION_ID)
{
echo "<BR>IN LOGINXML<BR>";
echo '<xml version="1.0">'.
'<response>log_in</response><parameters><error>'.$ERROR_ID.'</error><session>'
.$SESSION_ID.'</session></parameters></xml>';
}
I tried with header('Content-type: text/xml'); before echo, that doesnt work either. What can I do?

Simply remove the line
echo "<BR>IN LOGINXML<BR>";
XML should have only one root element, that is <xml> in your case

Related

Need to remove headers from cURL XML response in PHP

There's a few threads about this, but I couldn't find a solution to this issue in them. I hope it doesn't violate duplicate rules.
I've tested the following code with static XML and it works great, but said XML did not contain any headers.
I'm trying to remove headers through code after making a POST request so I can continue to process the resulting XML, but I'm not having any luck with it.
This is the XML:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AUTOS_Cotizar_PHPResponse xmlns="http://tempuri.org/"><AUTOS_Cotizar_PHPResult><auto xmlns=""><operacion>1555843</operacion><statusSuccess>TRUE</statusSuccess><statusText></statusText><cotizacion><cobertura><codigo>A0</codigo><descripcion>RESPONSABILIDAD CIVIL SOLAMENTE</descripcion><premio>928,45</premio><cuotas>01</cuotas><impcuotas>928,45</impcuotas></cobertura></cotizacion><datos_cotiz><suma>477250</suma><uso>901</uso></datos_cotiz></auto></AUTOS_Cotizar_PHPResult></AUTOS_Cotizar_PHPResponse></soap:Body></soap:Envelope>
this is the code:
//converting raw cURL response to XML
$temp1 = htmlspecialchars ($reply);
//replacing top headers
$temp2 = str_replace('<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AUTOS_Cotizar_PHPResponse xmlns="http://tempuri.org/"><AUTOS_Cotizar_PHPResult>', "<<<'EOD'", $temp1);
//replacing closing header tags
$temp3 = str_replace('</AUTOS_Cotizar_PHPResult></AUTOS_Cotizar_PHPResponse></soap:Body></soap:Envelope>', "EOD;", $temp2);
//this returns the original $temp1 without having anything replaced
echo $temp3;
//simplexml conversion
$xml = simplexml_load_string($temp3);
//running through the array and printing all values
if ($xml !== false) {
foreach ($xml->cotizacion as $cotizacion) {
foreach ($cotizacion->cobertura as $cobertura) {
echo $cobertura->codigo;
echo '<br>';
echo $cobertura->descripcion;
echo '<br>';
echo $cobertura->premio;
echo '<br>';
echo $cobertura->cuotas;
echo '<br>';
echo $cobertura->impcuotas;
echo '<br>';
}
}
}
There are probably more efficient ways to do this, or maybe I'm not doing this correctly. I'm just about learning right now, so feel free to correct me in any way if you want, I'd appreciate it!
The way you are processing the response string is a bad idea, you should stick to processing the content as XML and work with it. This uses XPath to find a start point to process the data (which I can't test with the current sample), but should help with what you need to do...
// Load the original reply
$xml = simplexml_load_string($reply);
//running through the array and printing all values
if ($xml !== false) {
// Find the <auto> element (use [0] as you want the first one)
$auto = $xml->xpath("//auto")[0];
// Loop through the cotizacion elements in the auto element
foreach ($auto->cotizacion as $cotizacion) {
foreach ($cotizacion->cobertura as $cobertura) {
echo $cobertura->codigo;
echo '<br>';
echo $cobertura->descripcion;
echo '<br>';
echo $cobertura->premio;
echo '<br>';
echo $cobertura->cuotas;
echo '<br>';
echo $cobertura->impcuotas;
echo '<br>';
}
}
}
The SOAP response is still an XML document, so work with it instead of fighting it. Treating it as a string is definitely not great.
As far as I can tell you're trying to work with all the <cotizaction> elements. It's simple to find elements inside an XML document. Read up on XPath.
$xml = simplexml_load_string(htmlspecialchars($reply));
if ($xml) {
foreach ($xml->xpath('//cotizacion') as $cotizacion) {
// do your thing
}
}

how to get html source without any error?

i want get the html source of a page but give a error!!
i use this code for give:
<?php
header('Content-Type: text/html; charset=utf-8');
$html = file_get_html("http://www.google.com/");
echo $html;
when I want to get the source from here I don't correct response and I get something like these characters
����i�[S$%ٲ�9������
Use the function file_get_contents instead:
http://www.php.net/manual/en/function.file-get-contents.php
you should do like this,note: Download simple_html_dom class here
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.google.co.in');
echo $html;
?>

How to echo <?xml version="1.0" encoding="utf-8"?> in a page?

I'm using a script to display a page in XML, but I need it to display at start but whenever I try, it breaks the code.
Heres the code I tried:
<?php
header('Content-Type: application/xml');
$output = "
<?xml version='1.0' encoding='utf-8' ?>
";
print ($output);
?>
but its not working, any help ?
use echo , Basic Simple XML
<?php
header('Content-Type: application/xml');
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo $output;
?>
yes, use echo, but you don't need additional variable and you can use single quote string without backslash...
<?php
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?' . ">\n"; //connate strings to protect end of php formatting in simple editors, and use new line tag
?>

PHP looping through XML error: Trying to get property of non-object

function loadFilesToArrays(){
$notpromo1Array = simplexml_load_file('pages/hegnar/source/1/notpromo-1_08_04_2013_1.xml');
foreach($notpromo1Array as $xml){
echo $xml -> getName();
echo "<br>";
echo $xml -> ORDREHODE -> SluttkundeNr;
echo "<br>";
}
}
My XML look like this
<?xml version="1.0" encoding="UTF-8"?>
<IS_DATA>
<ORDRER class="linked-list">
<ORDREHODE>
<ORDREKUNDENR>10541</ORDREKUNDENR>
<SluttkundeNr>1240</SluttkundeNr>
<AND OTHER PROPERTIES></AND OTHER PROPERTIES>..........
</ORDREHODE>
<ORDREHODE>
<ORDREKUNDENR>10541</ORDREKUNDENR>
<SluttkundeNr>1344</SluttkundeNr>
<AND OTHER PROPERTIES></AND OTHER PROPERTIES>..........
</ORDREHODE>
<ORDREHODE>
ETC ETC ETC ..................
</ORDREHODE>
</ORDRER>
</IS_DATA>
And the XML is properly ended, etc, I used notepad++'s xml validator.
I don't understand why when I call loadFilesToArray function, I get this error Notice: Trying to get property of non-object in /path/to/file/page1.php on line 104
The IS_DATA is after all enclosed in curly brackets and single quotes as I have seen lots of people referring to when wanting to echo data from XML having special characters in property names in XML.
Please give correct path of your xml file right now a.xml and test.php both in same folder
a.xml
<?xml version="1.0" encoding="UTF-8"?>
<IS_DATA>
<ORDRER class="linked-list">
<ORDREHODE>
<ORDREKUNDENR>10541</ORDREKUNDENR>
<SluttkundeNr>1240</SluttkundeNr>
<ANDOTHERPROPERTIES></ANDOTHERPROPERTIES>
</ORDREHODE>
<ORDREHODE>
<ORDREKUNDENR>10541</ORDREKUNDENR>
<SluttkundeNr>1344</SluttkundeNr>
<ANDOTHERPROPERTIES></ANDOTHERPROPERTIES>
</ORDREHODE>
<ORDREHODE>
ETC ETC ETC ..................
</ORDREHODE>
</ORDRER>
</IS_DATA>
test.php
<?php
$notpromo1Array = simplexml_load_file("a.xml");
foreach($notpromo1Array as $xml)
{
foreach($xml as $child)
{
echo $child->getName();
echo "<br>";
echo $child->SluttkundeNr;
echo "<br>";
}
}
/*RESULT
* ORDREHODE
1240
ORDREHODE
1344
ORDREHODE
*/
?>
You need to debug your code:
foreach ($notpromo1Array as $xml)
{
echo $xml->getName(), "\n";
}
This gives you the name of the element represented by $xml:
ORDRER
As this element does not have any <IS_DATA> child, simplexml gives you NULL. And then you access ->ORDRER on NULL which does not work as it is not an object:
Notice: Trying to get property of non-object
That simple it is. Just access the correct elements and you're fine:
foreach ($notpromo1Array as $xml)
{
echo $xml->getName(), "\n";
echo $xml->ORDREHODE->SluttkundeNr, "\n";
}
Output:
ORDRER
1240
just made a quick test and here's what I came up with. Replace your foreach code for this one:
foreach($notpromo1Array as $xml){
echo $xml->ORDREHODE->SluttkundeNr;
echo "<br />";
}
FYI, I used the following XML file as example and it printed 1240:
<?xml version="1.0" encoding="UTF-8"?>
<IS_DATA>
<ORDRER class="linked-list">
<ORDREHODE>
<ORDREKUNDENR>10541</ORDREKUNDENR>
<SluttkundeNr>1240</SluttkundeNr>
</ORDREHODE>
</ORDRER>
</IS_DATA>

getting data from php variable

$content = file_get_contents('file.php');
echo $content;
nothing displays, expect when displaying the page sourcecode in browser the display is this
<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
so it wont execute the script when getting the content..
file.php contains this code
<? foreach(glob("folder/*.php") as $class_filename) {
require_once($class_filename);
}
?>
and if I do next
$content = foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
it complains about unexpected foreach...
is there a way to read the folder/.php files content to single $variable and then echo/print all folder/.php files to page where it should be?
thanks for help already.
Is that what you want to do ?
$content = '';
foreach (glob('folder/*.php') as $class){$content .= file_get_contents($class);}
echo $content;
What you're trying won't execute the contents of the "file.php", jsut display the contents of them on screen.
If you want to execute file.php, use eval ($content)
To capture the output, use something like:
ob_start(); // Don't echo anything but buffer it up
$codeToRun=file_get_contents('file.php'); // Get the contents of file.php
eval ($codeToRun); // Run the contents of file.php
$content=ob_get_flush(); // Dump anything that should have been echoed to a variable and stop buffering
echo $content; //echo the stuff that should have been echoed above

Categories