Essentially, I currently want to parse the XML data that was generated from the html and this was working fine but for some reason will not parse now...gives date of jan 01 1970??...any ideas of problem as coding seems fine?
Thanks a lot in advance!
<?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
Host: $proxy_name
");
while(!feof($proxy_fp)) {$proxy_cont .= fread($proxy_fp,4096);}
fclose($proxy_fp);
$proxy_cont = substr($proxy_cont, strpos($proxy_cont,"
")+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'><b>".$car->cartype."</b><br>".$car->carsipp."<br>".$car->transmission."</td><td><b>".$car->carexample."</b></td><td><b>£".$car->price."
</b><br>Unlimited Miles</b><br>
<input type=button onclick=\"javascript:newWin('".trim($car->book)."');\" value='Prepay Now'></td></tr>";
}
echo "</table>";
?>
a date of "jan 01 1970" means that the date was either passed in null or as 0 ... check the raw value of your post to make sure it's coming in as expected.
There's a lot of room for improvement on your code but I'll just stick to the problem your reported. My advice would be for you to change your disp_date function
to:
function disp_date($str) {
return date("M d, Y", strtotime($str));
}
I just picked up a subset of the XML that URL returns and created this small code snippet for demo purposes:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<Quote>
<hire>
<loccode>AI001</loccode>
<locname>LONDON - HEATHROW AIRPORT</locname>
<pickupdate>20130819</pickupdate>
<pickuptime>09:00</pickuptime>
<dropbackdate>20130823</dropbackdate>
<dropbacktime>09:00</dropbacktime>
</hire>
</Quote>
XML;
$sxe = new SimpleXMLElement($xml);
echo date("M d, Y",strtotime($sxe->hire->pickupdate));
Aug 19, 2013