dongxinjun3944 2012-05-12 19:10
浏览 81
已采纳

使用php抓取xml数据并将其打印到div时的空白页面

I tried this for pulling out the data from xml to php in session and printing in div but it shows blank page.. I tried it different ways.. below is another way of printing but still same result..

<?php
session_start();

$yql_base_url = "http://query.yahooapis.com/v1/public/yql";  
$yql_query = "select * from yahoo.finance.quotes where symbol in ('XAUUSD=X','XAGUSD=X','PLN12.NYM','PAK12.NYM','USDCAD=X')";  
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&env=http://datatables.org/alltables.env";
$yql_query_url .= "&format=json";
$session = curl_init($yql_query_url);  
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);      
$json = curl_exec($session);
$phpObj =  json_decode($json);  
if(!is_null($phpObj->query->results)){  
}  
if(!is_null($phpObj->query->results)){  
    foreach($phpObj->query->results->quote as $quotes){  
        $quotes = $_SESSION[''];
        $_SESSION['Ask'] = $Ask;
        $_SESSION['Bid'] = $Bid;
        $_SESSION['AskRealtime'] = $AskRealtime;
        $_SESSION['BidRealtime'] = $BidRealtime;
        $_SESSION['LastTradeDate'] = $LastTradeDate;
        $_SESSION['LastTradePriceOnly'] = $LastTradePriceOnly;
        $_SESSION['LastTradeTime'] = $LastTradeTime;
  }  
}  
?>
<body>
<div>
<ul>
<li><?php echo $_SESSION['Ask'];?></li>
<li><?php echo $_SESSION['Bid'];?></li>
<li><?php echo $_SESSION['AskRealtime'];?></li>
<li><?php echo $_SESSION['BidRealtime'];?></li>
<li><?php echo $_SESSION['LastTradeDate'];?></li>
<li><?php echo $_SESSION['LastTradePriceOnly'];?></li>
<li><?php echo $_SESSION['LastTradeTime'];?></li>
</ul>
</div>
</body>

Can any one suggest please

I even tried editing the section "foreach($...

foreach($phpObj->query->results->quote as $quotes){  
        echo $quotes['Ask'];
        echo $quotes['Bid'];
        echo $quotes['AskRealtime'];
        echo $quotes['BidRealtime'];
        echo $quotes['LastTradeDate'];
        echo $quotes['LastTradePriceOnly'];
        echo $quotes['LastTradeTime'];
        }  

Thanks in advance
XML Generated by YQL

<query yahoo:count="5" yahoo:created="2012-05-12T21:27:10Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<redirect from="http://datatables.org/alltables.env" status="301">http://www.datatables.org/alltables.env</redirect>
<url execution-start-time="404" execution-stop-time="830" execution-time="426" proxy="DEFAULT">http://datatables.org/alltables.env</url>
<url execution-start-time="846" execution-stop-time="1256" execution-time="410" proxy="DEFAULT">http://www.datatables.org/yahoo/finance/yahoo.finance.quotes.xml</url>
<url execution-start-time="1464" execution-stop-time="1465" execution-time="1" proxy="DEFAULT">http://download.finance.yahoo.com/d/quotes.csv?f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy&s=XAUUSD%3DX,XAGUSD%3DX,PLN12.NYM,PAK12.NYM,USDCAD%3DX</url>
<query execution-start-time="1262" execution-stop-time="1465" execution-time="203" params="{url=[http://download.finance.yahoo.com/d/quotes.csv?f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy&s=XAUUSD%3DX,XAGUSD%3DX,PLN12.NYM,PAK12.NYM,USDCAD%3DX]}">select * from csv where url=@url and columns='Ask,AverageDailyVolume,Bid,AskRealtime,BidRealtime,BookValue,Change&PercentChange,Change,Commission,ChangeRealtime,AfterHoursChangeRealtime,DividendShare,LastTradeDate,TradeDate,EarningsShare,ErrorIndicationreturnedforsymbolchangedinvalid,EPSEstimateCurrentYear,EPSEstimateNextYear,EPSEstimateNextQuarter,DaysLow,DaysHigh,YearLow,YearHigh,HoldingsGainPercent,AnnualizedGain,HoldingsGain,HoldingsGainPercentRealtime,HoldingsGainRealtime,MoreInfo,OrderBookRealtime,MarketCapitalization,MarketCapRealtime,EBITDA,ChangeFromYearLow,PercentChangeFromYearLow,LastTradeRealtimeWithTime,ChangePercentRealtime,ChangeFromYearHigh,PercebtChangeFromYearHigh,LastTradeWithTime,LastTradePriceOnly,HighLimit,LowLimit,DaysRange,DaysRangeRealtime,FiftydayMovingAverage,TwoHundreddayMovingAverage,ChangeFromTwoHundreddayMovingAverage,PercentChangeFromTwoHundreddayMovingAverage,ChangeFromFiftydayMovingAverage,PercentChangeFromFiftydayMovingAverage,Name,Notes,Open,PreviousClose,PricePaid,ChangeinPercent,PriceSales,PriceBook,ExDividendDate,PERatio,DividendPayDate,PERatioRealtime,PEGRatio,PriceEPSEstimateCurrentYear,PriceEPSEstimateNextYear,Symbol,SharesOwned,ShortRatio,LastTradeTime,TickerTrend,OneyrTargetPrice,Volume,HoldingsValue,HoldingsValueRealtime,YearRange,DaysValueChange,DaysValueChangeRealtime,StockExchange,DividendYield'</query>
<javascript execution-time="260" instructions-used="302744" table-name="yahoo.finance.quotes"/>
<warning>Unable to sort response according to field 'Ask,Bid,AskRealtime,BidRealtime,LastTradeDate,LastTradePriceOnly,LastTradeTime'.</warning>
<user-time>1525</user-time>
<service-time>837</service-time>
<build-version>27222</build-version>
</diagnostics>
<results>
<quote symbol="XAUUSD=X">
  <Bid>1585.30</Bid>
  <AskRealtime/>
  <BidRealtime/>
  <LastTradeDate>5/11/2012</LastTradeDate>
  <LastTradePriceOnly>1580.3002</LastTradePriceOnly>
  <LastTradeTime>5:15pm</LastTradeTime>
  <Ask>1575.30</Ask>
</quote>
<quote symbol="XAGUSD=X">
  <Bid/>
  <AskRealtime/>
  <BidRealtime/>
  <LastTradeDate>5/11/2012</LastTradeDate>
  <LastTradePriceOnly>28.858</LastTradePriceOnly>
  <LastTradeTime>5:39pm</LastTradeTime>
  <Ask/>
</quote>
<quote symbol="PLN12.NYM">
  <Bid/>
  <AskRealtime>1478.60</AskRealtime>
  <BidRealtime>1462.90</BidRealtime>
  <LastTradeDate>5/11/2012</LastTradeDate>
  <LastTradePriceOnly>1465.30</LastTradePriceOnly>
  <LastTradeTime>5:02pm</LastTradeTime>
  <Ask/>
</quote>
<quote symbol="PAK12.NYM">
  <Bid/>
  <AskRealtime>0.00</AskRealtime>
  <BidRealtime>0.00</BidRealtime>
  <LastTradeDate>3/23/2012</LastTradeDate>
  <LastTradePriceOnly>658.80</LastTradePriceOnly>
  <LastTradeTime>7:12am</LastTradeTime>
  <Ask/>
</quote>
<quote symbol="USDCAD=X">
  <Bid>1.0005</Bid>
  <AskRealtime/>
  <BidRealtime/>
  <LastTradeDate>5/11/2012</LastTradeDate>
  <LastTradePriceOnly>1.001</LastTradePriceOnly>
  <LastTradeTime>5:55pm</LastTradeTime>
  <Ask>1.0015</Ask>
</quote>

I Want to grab all the Ask, Bid, AskRealtime... value from all < quote symbol=".." >

  • 写回答

1条回答 默认 最新

  • dongpo5264 2012-05-12 19:35
    关注

    Add this line before curl_init

    $yql_query_url .= "&env=http://datatables.org/alltables.env";
    

    If you do this, the returned data will be properly parsed into $phpObj. The reason why you get a blank page now is because you never set the $Ask, $Bid, $AskRealTime, etc. variables - but you assign them to your session variable.

    To fix that, delete this line:

    $quotes = $_SESSION[''];
    

    and change each of the following lines to be like this:

    $_SESSION['Ask'] = $quotes->Ask;
    

    I don't understand what you're trying to do or how you're trying to store the data - these session variables as they are will be overwritten every iteration of the loop. Anyway, that's your business. I've shown you how you can extract the data properly. You can figure out how you want to store it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题