将URL生成的XML数据转换为表 [英] Convert the XML data generated by a URL into a table

查看:65
本文介绍了将URL生成的XML数据转换为表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我目前拥有从URL生成的XML数据,但希望将其转换为表格。由于某种原因不起作用。还启用了PHP错误查找并提出以下内容:



警告:simplexml_load_string():实体:第2行:解析器错误:仅在开始时允许XML声明第43行/customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php中的文档警告:simplexml_load_string():in /customers/f/4/2/wheels4rent.net/httpd.www/第43行的list_car1.php警告:第43行的/customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php中的simplexml_load_string():^警告:fgets()期望参数1为资源,字符串在第100行/customers/f/4/2/wheels4rent.net/httpd.www/list_car1.php中给出



生成XML的URL是:

< 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&安培; xdbd ay = 19& xdbtime = 09:00& xclass = M 





此URL应将XML内容放入list_car1.php的表中如下所示,但没有任何反应......请查看www.wheels4rent.net并输入日期以查看问题





<?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 - >;


函数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);
返回$ 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\\\
Host:$ proxy_name \\\\\\\ \
);
while(!feof($ proxy_fp)){$ proxy_cont。= fread($ proxy_fp,4096);}
fclose($ proxy_fp);
$ proxy_cont = substr($ proxy_cont,strpos($ proxy_cont,\\\\\\\ n)+ 4);
返回$ proxy_cont;
}

echo proxy_url($ string); * /


函数XML2Array($ xml,$ recursive = false)
{
if(!$ recursive)
{
$ array = simplexml_load_string($ xml);
}
其他
{
$ 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);
}
}
返回$ newArray;
}
函数disp_date($ str)
{
$ y = substr($ str,0,4);
$ m = substr($ str,4,2);
$ d = substr($ str,6,2);
// echo $ y。 - 。$ m。 - 。$ d;
返回日期(M d,Y,strtotime($ y。 - 。$ m。 - 。$ d));
}


$ handle = fgets($ string,r);
$ xml_string =;
//如果有什么东西,请阅读并返回
if($ handle){
while(!feof($ handle)){
$ buffer = fgets($ handle, 4096);
$ xml_string。= $ buffer;
}
fclose($ handle);
}

// $ xmlDoc = new DOMDocument();
// echoxml string =。 $ xml_string;

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

// print_r($ xmlDoc);

echo< br>< strong />拿起地点:。$ xmlDoc-> hire-> loccode。< br>下车地点:。$ xmlDoc-> hire-> locdrop。< br>取件时间:。disp_date($ xmlDoc-> hire-> pickupdate)。。$ xmlDoc-> hire-> pickuptime。< ; br>回拨时间:。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>速率< / 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' >的 $小车 - > cartype <峰; br> 中。$小车 - > carsipp。 <峰; br> 中。$小车 - >传输 < / td>< td> 。$ car-> carexample。< / td>< td> £。$ car-> price。
< br>无限里程< br>
< input type =buttonönclick= \javascript:newWin('。trim($ car-> book)。'); \\ \\value ='预付现金'>< / td>< / tr>;


}



echo< / table>;



?>

解决方案

string =http:// www.thrifty.co.uk/cgi-bin/gen5?runprog=thxml&xsrc=7qhfqou3&mode=quote ;

字符串= &安培; XLOC =
_REQUEST [ loccode];

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>";



?>

解决方案

string="http://www.thrifty.co.uk/cgi-bin/gen5?runprog=thxml&xsrc=7qhfqou3&mode=quote";


string.="&xloc=".


_REQUEST["loccode"];


这篇关于将URL生成的XML数据转换为表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆