0

我尝试在会话中将数据从 xml 提取到 php 并在 div 中打印,但它显示空白页.. 我尝试了不同的方式.. 下面是另一种打印方式,但结果仍然相同..

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

有人可以建议吗

我什至尝试编辑“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'];
        }  

在此先感谢
XML 由 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>

我想从所有 <quote symbol=".." > 中获取所有 Ask、Bid、AskRealtime... 值

4

1 回答 1

0

在 curl_init 之前添加这一行

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

如果这样做,返回的数据将被正确解析为 $phpObj。你现在得到一个空白页面的原因是因为你从未设置 $Ask、$Bid、$AskRealTime 等变量 - 但你将它们分配给你的会话变量。

要解决此问题,请删除此行:

$quotes = $_SESSION[''];

并将以下每一行更改为:

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

我不明白您要做什么或您要如何存储数据-这些会话变量原样将在循环的每次迭代中被覆盖。不管怎样,那是你的事。我已经向您展示了如何正确提取数据。你可以弄清楚你想如何存储它。

于 2012-05-12T19:35:07.960 回答