从HTML解析XML查询 [英] parsing XML queries from HTML

查看:65
本文介绍了从HTML解析XML查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我目前想要解析从html生成的XML数据,这样工作正常但由于某种原因现在不会解析...给出1970年1月1日的日期??。 ..编码问题的任何想法似乎都很好? 

非常感谢!

< pre lang =PHP><<>?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' >< b> 中$小车 - > cartype。 < / b><峰; br> 中$小车 - > carsipp。 <峰; br> 中。$小车 - >传输 < ; / TD>< TD>< b>中。$小车 - > carexample。 < / b>< / TD>< TD>< b取代;&安培;磅; $小车 - > ;价格。
< / b>< br>无限里程< / b>< br>
<输入类型=按钮onclick = \javascript:newWin('。trim ($ car-> book)。'); \value ='预付现金'>< / td>< / tr>;


}



echo< / table>;



?>

php

解决方案

string =http://www.thrifty.co.uk/cgi-箱/ GEN5 runprog = thxml&安培; xsrc = 7qhfqou3&安培;模式=报价 ;

字符串= &安培; XLOC =

_REQUEST [ loccode];

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!

<pre lang="PHP"><<pre>?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'><b>".$car->cartype."</b><br>".$car->carsipp."<br>".$car->transmission."</td><td><b>".$car->carexample."</b></td><td><b>&pound;".$car->price."
</b><br>Unlimited Miles</b><br>
<input type=button onclick=\"javascript:newWin('".trim($car->book)."');\" value='Prepay Now'></td></tr>";


}



echo "</table>";



?>

php 

解决方案

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


string.="&xloc=".


_REQUEST["loccode"];


这篇关于从HTML解析XML查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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