0

这是我用来从 yahoo Finance 股票报价中获取数据的脚本。但我在这里面临问题..并且无法从我得到的答案中找到解决方案。PHP 脚本是...

<?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){  
        $_SESSION['Ask'] = $quotes->Ask;
        $_SESSION['Bid'] = $quotes->Bid;
        $_SESSION['AskRealtime'] = $quotes->AskRealtime;
        $_SESSION['BidRealtime'] = $quotes->BidRealtime;
        $_SESSION['LastTradeDate'] = $quotes->LastTradeDate;
        $_SESSION['LastTradePriceOnly'] = $quotes->LastTradePriceOnly;
        $_SESSION['LastTradeTime'] = $quotes->LastTradeTime;
  }  
}  
?>

我打印会话的显示部分或正文部分是......

    <body>
    <div>
    <ul>
    <li>Gold Price</li>
    <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>
    <ul>
    <li>Silver Price</li>
    <li><?php echo $_SESSION['Ask_2'];?></li>
    <li><?php echo $_SESSION['Bid_2'];?></li>
    <li><?php echo $_SESSION['AskRealtime_2'];?></li>
    <li><?php echo $_SESSION['BidRealtime_2'];?></li>
    <li><?php echo $_SESSION['LastTradeDate_2'];?></li>
    <li><?php echo $_SESSION['LastTradePriceOnly_2'];?></li>
    <li><?php echo $_SESSION['LastTradeTime_2'];?></li>
    </ul>
    <ul>
    <li>Platinum Price</li>
    <li><?php echo $_SESSION['Ask_3'];?></li>
    <li><?php echo $_SESSION['Bid_3'];?></li>
    <li><?php echo $_SESSION['AskRealtime_3'];?></li>
    <li><?php echo $_SESSION['BidRealtime_3'];?></li>
    <li><?php echo $_SESSION['LastTradeDate_3'];?></li>
    <li><?php echo $_SESSION['LastTradePriceOnly_3'];?></li>
    <li><?php echo $_SESSION['LastTradeTime_3'];?></li>
    </ul>
    <ul>
    <li>Palladium Price</li>
    <li><?php echo $_SESSION['Ask_3'];?></li>
    <li><?php echo $_SESSION['Bid_3'];?></li>
    <li><?php echo $_SESSION['AskRealtime_3'];?></li>
    <li><?php echo $_SESSION['BidRealtime_3'];?></li>
    <li><?php echo $_SESSION['LastTradeDate_3'];?></li>
    <li><?php echo $_SESSION['LastTradePriceOnly_3'];?></li>
    <li><?php echo $_SESSION['LastTradeTime_3'];?></li>
    </ul>
    <ul>
    <li>USD to CAD</li>
    <li><?php echo $_SESSION['Ask_3'];?></li>
    <li><?php echo $_SESSION['Bid_3'];?></li>
    <li><?php echo $_SESSION['AskRealtime_3'];?></li>
    <li><?php echo $_SESSION['BidRealtime_3'];?></li>
    <li><?php echo $_SESSION['LastTradeDate_3'];?></li>
    <li><?php echo $_SESSION['LastTradePriceOnly_3'];?></li>
    <li><?php echo $_SESSION['LastTradeTime_3'];?></li>
    </ul>
    </div>
    </body>

但我从上面得到的结果是..
1.0015 1.0005
5/11/2012
1.001
5:55pm

这只是$yql_query 的 'USDCAD =X'的会话输出= "select * from ...'PLN12.NYM','PAK12.NYM', 'USDCAD =X' )";

查询是..

$yql_query = "select * from yahoo.finance.quotes where symbol in ('XAUUSD=X','XAGUSD=X','PLN12.NYM','PAK12.NYM','USDCAD=X')";

来自 yahoo 网站的 yahoo Finance 报价的 XML 输出是。

<query yahoo:count="5" yahoo:created="2012-05-12T22:01:43Z" 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="417" execution-stop-time="775" execution-time="358" proxy="DEFAULT">http://datatables.org/alltables.env</url>
  <url execution-start-time="826" execution-stop-time="1040" execution-time="214" proxy="DEFAULT">http://www.datatables.org/yahoo/finance/yahoo.finance.quotes.xml</url>
  <url execution-start-time="1247" execution-stop-time="1261" execution-time="14" 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="1045" execution-stop-time="1261" execution-time="216" 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="279" 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>1326</user-time>
  <service-time>586</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>

有关 XML 的清晰视图,请单击此处

我想提取指定 div 上的所有 Ask 价格、Bid 价格……等等。

请提出建议

4

2 回答 2

0

您没有正确存储数据。您想打印出 $_SESSION['Ask_2'] 但它从未设置!相反,在循环中,您不断覆盖 $_SESSION['Ask'] - 您可能想要做的是获取引号符号,然后将数据存储在 $_SESSION[$symbol]['Ask'] 中。

事实上,我建议使用另一个数组来编译数据,然后在其中设置一个包含该数组的会话变量,而不是不断更新会话变量。

你想要一个这样的循环:

foreach($phpObj->query->results->quote as $quote){  
    $mydata[$quote->Symbol]['Ask'] = $quote->Ask;

然后当你显示结果时,显示 fromforeach ($mydata as $quote)而不是$_SESSION. 最后,您可以说$_SESSION['quotedata'] = $mydata是否要将所有内容存储在会话中。

于 2012-05-14T18:25:19.023 回答
0

我还建议您将其显示在表格中而不是 .

使用下面的解决方案,您只需从符号列表中添加/减去值,表格将自动更改。

即,如果你改变了

$yql_query = "select * from yahoo.finance.quotes where symbol in ('XAUUSD=X','XAGUSD=X','PLN12.NYM','PAK12.NYM','USDCAD=X')";

$yql_query = "select * from yahoo.finance.quotes where symbol in ('XAUUSD=X','XAGUSD=X','PLN12.NYM','PAK12.NYM','USDCAD=X','BOOM')";

那么您的表格将只多出一行股票代码 BOOM。我对此进行了测试,并且可以正常工作。

这是您的数据收集部分,其中包含 Ansari 所做的更正:

$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)){ // this does nothing. remove it?
}

if(!is_null($phpObj->query->results)){  
    foreach($phpObj->query->results->quote as $quotes){  
        $mydata[$quotes->Symbol]['Ask'] = $quotes->Ask;
        $mydata[$quotes->Symbol]['Bid'] = $quotes->Bid;
        $mydata[$quotes->Symbol]['AskRealtime'] = $quotes->AskRealtime;
        $mydata[$quotes->Symbol]['BidRealtime'] = $quotes->BidRealtime;
        $mydata[$quotes->Symbol]['LastTradeDate'] = $quotes->LastTradeDate;
        $mydata[$quotes->Symbol]['LastTradePriceOnly'] = $quotes->LastTradePriceOnly;
        $mydata[$quotes->Symbol]['LastTradeTime'] = $quotes->LastTradeTime;
  }
}

这是表格形式的输出:

<table border="1">
<thead>
<tr>
<?php
$head = array('Symbol','Ask','Bid','Ask Realtime','Bid Realtime','Last Trade Date','Last Trade Price','Last Trade Time');
foreach($head as $value) {
    echo '<th>' . $value . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
foreach($mydata as $symbol=>$attrib) {
    echo '<tr>';
    echo '<td>' . $symbol . '</td>';
    foreach($attrib as $val) {
        echo '<td>' . $val . '</td>';
    }
    echo '</tr>';
}
?>
</tbody>
</table>
于 2012-08-11T20:30:43.770 回答