Click here to Skip to main content
15,996,848 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Essentially, I currently have XML data that is generated from a URL but want this converted to a table. For some reason does not work. have also enabled PHP error finding and comes up with following :

Warning: simplexml_load_string(): Entity: line 2: parser error : XML declaration allowed only at the start of the document in /customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php on line 43 Warning: simplexml_load_string(): in /customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php on line 43 Warning: simplexml_load_string(): ^ in /customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php on line 43 Warning: fgets() expects parameter 1 to be resource, string given in /customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php on line 100

The URL that generates the XML is:
<a href="http://www.thrifty.co.uk/cgi-bin/gen5?runprog=thxml&xsrc=7qhfqou3&mode=quote&xloc=AI001&xlocname=&xlocdrop=&xbook=&xonewaystart=&xonewayend=&xpuyear=2013&xpumonth=08&xpuday=12&xputime=09:00&xdbyear=2013&xdbmonth=08&xdbday=19&xdbtime=09:00&xclass=M


This URL should place XML content into a table from list_car1.php as shown below but nothing happens...please check www.wheels4rent.net and enter dates to see problem


<?php
//ini_set("disable_functions",null);
//phpinfo();

$string="http://www.thrifty.co.uk/cgi-bin/gen5?runprog=thxml&xsrc=7qhfqou3&mode=quote";
$string.="&xloc=".$_REQUEST["loccode"];
$string.="&xlocname=".$_REQUEST["locname"];
$string.="&xlocdrop=".$_REQUEST["locdrop"];
$string.="&xbook=".$_REQUEST["book"];
$string.="&xonewaystart=".$_REQUEST["onewaystart"];
$string.="&xonewayend=".$_REQUEST["onewayend"];
$string.="&xpuyear=".date("Y",strtotime($_POST['pickup_date']));
$string.="&xpumonth=".date("m",strtotime($_POST['pickup_date']));
$string.="&xpuday=".date("d",strtotime($_POST['pickup_date']));
$string.="&xputime=".$_REQUEST["pu_time"];
$string.="&xdbyear=".date("Y",strtotime($_POST['return_date']));
$string.="&xdbmonth=".date("m",strtotime($_POST['return_date']));
$string.="&xdbday=".date("d",strtotime($_POST['return_date']));
$string.="&xdbtime=".$_REQUEST["db_time"];
$string.="&xclass=".$_REQUEST["vehicle_type"];

echo "<!-- $string -->";


function get_data($url)
{
 
 
  echo $url;
 
 
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
//echo get_data($string);
$xmlDoc=simplexml_load_string ( get_data($string) ) ;
/*
function proxy_url($proxy_url)
{
    $proxy_name = '127.0.0.1';
    $proxy_port = 4001;
    $proxy_cont = '';

    $proxy_fp = fsockopen($proxy_name, $proxy_port);
    if (!$proxy_fp)    {return false;}
    fputs($proxy_fp, "GET $proxy_url HTTP/1.0\r\nHost: $proxy_name\r\n\r\n");
    while(!feof($proxy_fp)) {$proxy_cont .= fread($proxy_fp,4096);}
    fclose($proxy_fp);
    $proxy_cont = substr($proxy_cont, strpos($proxy_cont,"\r\n\r\n")+4);
    return $proxy_cont;
} 

echo proxy_url($string);*/


function XML2Array ( $xml , $recursive = false )
{
    if ( ! $recursive )
    {
        $array = simplexml_load_string ( $xml ) ;
    }
    else
    {
        $array = $xml ;
    }
   
    $newArray = array () ;
    $array = ( array ) $array ;
    foreach ( $array as $key => $value )
    {
        $value = ( array ) $value ;
        if ( isset ( $value [ 0 ] ) )
        {
            $newArray [ $key ] = trim ( $value [ 0 ] ) ;
        }
        else
        {
            $newArray [ $key ] = XML2Array ( $value , true ) ;
        }
    }
    return $newArray ;
}
function disp_date($str)
{
$y=substr($str,0,4);
$m=substr($str,4,2);
$d=substr($str,6,2);
//echo $y."-".$m."-".$d;
return date("M d, Y",strtotime($y."-".$m."-".$d));
}


$handle = fgets($string, "r");
$xml_string="";
// If there is something, read and return
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        $xml_string.=$buffer;
    }
    fclose($handle);
}

//$xmlDoc = new DOMDocument();
//echo "xml string = " . $xml_string;

?></div>
																			<div class="box">
																			<?

//print_r($xmlDoc);

echo "<br><strong/>Pick up Location: ".$xmlDoc->hire->loccode."<br> Drop-off Location: ".$xmlDoc->hire->locdrop."<br>Pickup Time: ".disp_date($xmlDoc->hire->pickupdate)." ".$xmlDoc->hire->pickuptime."<br>Dropback Time: ".disp_date($xmlDoc->hire->dropbackdate)." ".$xmlDoc->hire->dropbacktime."<br>";

echo "<table border=1 style='font:12px verdana'  cellspacing=0 cellpadding=3><tr><td>Car Type</td><td>Description</td><td>Rate</td></tr>";


foreach($xmlDoc->car as $car)
{
$url = $car->book;  
$url = str_replace('wheels4rent.net', '', '$url'); 
echo "<!-- url = $car->book -->";
echo "<tr><td width=200px><img src='".$car->carimage."' align='left' style='padding:1px; width:100px'>".$car->cartype."<br>".$car->carsipp."<br>".$car->transmission."</td><td>".$car->carexample."</td><td>£".$car->price."
<br>Unlimited Miles<br>
<input type=button  önclick=\"javascript:newWin('".trim($car->book)."');\" value='Prepay Now'></td></tr>";


}



echo "</table>";



?>
Posted
Updated 1-Aug-13 11:28am
v2

1 solution

What the error says is that the html document header has been sent, and that you can not initialize an XML document after that. (As I read that error). Try removing all the echo statements and compile a string to do that and echo it at then end of the script. At a closer look it seems to be only one echo in the first function that causes the document header to be sent.
 
Share this answer
 
v2
Comments
Member 8230208 2-Aug-13 5:09am    
I can see that the URL generated from inputting data seems correct and when I 'echo $url;' this gives me www.thrifty.co.uk...... but the XML content does not seem convert to table and this did work before so cannot find why this is happening....my code sems fine...cannot point where problems is
Lars Fisker 2-Aug-13 17:16pm    
Looked up the error message you provided at the top. That looks like the line at the top the xml file isn't a valid XML Declaration. Try linking the file and I will take a look.
Member 8230208 17-Aug-13 16:00pm    
if one copies the URL www.thrifty.co.uk....etc and paste this will give an XML list which you can see with 'quote' and 'book' tags but the code that is written in list_car1.php should take care of that. Could u possibly take a closer look if I gave u host details so that u could log in?
Member 8230208 24-Aug-13 5:13am    
could u take a closer look if I gave u hosting details as this has stumped me?
Member 8230208 10-Aug-13 5:15am    
errors I have are following:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 2: parser error : XML declaration allowed only at the start of the document in /home/a2039099/public_html/list_car1.php on line 330


Warning: simplexml_load_string() [function.simplexml-load-string]: in /home/a2039099/public_html/list_car1.php on line 330

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/a2039099/public_html/list_car1.php on line 330

Warning: fgets(): supplied argument is not a valid stream resource in /home/a2039099/public_html/list_car1.php on line 387

Warning: Invalid argument supplied for foreach() in /home/a2039099/public_html/list_car1.php on line 412

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900