2014-02-12 45 views
0

我想获取国家证券交易所数据并使用Google图表创建图表。但我无法解析如何解析使用api的数据。这是api的代码。任何帮助将不胜感激。输出看起来像这样http://theawesomecoder.com/calc/chart.php解析国家证券交易所数据并创建图表

<?php 
$curlSession = curl_init(); 
curl_setopt($curlSession, CURLOPT_URL, 'http://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-10007&symbol=NIFTY&symbol=NIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17'); 
curl_setopt($curlSession,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true); 
$homepage = curl_exec($curlSession); 



var_dump($homepage); 

curl_close($curlSession); 
?> 
+0

输出结果如何? – Lloyd

+0

您对哪一栏数据感兴趣? – Fabricio

+0

我已经把链接的输出看起来像http://theawesomecoder.com/calc/chart.php –

回答

0

如果要做好这方面的工作,你应该做的这一切都在服务器端,并为请参考此答案:

How do you parse and process HTML/XML in PHP?

否则这应该以更快的方式解决您的问题:

<html> 
<head> 
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"> 
    <script src="//code.jquery.com/jquery-1.11.0.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(window).load(function(){ 
     var strikePrice = new Array(); 
     $(".opttbldata").find("tr").each(function (row, currentTR) { 
      if (row > 1) { // Avoid first two header rows (0 based index) 
       var columnContent = $(currentTR).find("td:eq(6)").text(); 
       strikePrice[row-2] = (columnContent=="-"?" 0":columnContent); // Strike price column (0 based index) 
      } 
     }); 
     $("#NSEIndiaData").remove(); // Get rid of the loaded page/data to uncluter the DOM 
     alert(strikePrice); 
     }); 
    </script> 
</head> 

<body> 
    <?php 
     $curlSession = curl_init(); 
     curl_setopt($curlSession, CURLOPT_URL, 'http://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-10007&symbol=NIFTY&symbol=NIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17'); 
     curl_setopt($curlSession,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
     curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true); 
     curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true); 
     $homepage = curl_exec($curlSession); 
    ?> 

    <div id="NSEIndiaData" style="display: none;"> 
     <?php var_dump($homepage); ?> 
    </div> 

    <?php 
     curl_close($curlSession); 
    ?> 
</body> 
</html> 

第12列的所有值将放置在基于0的索引阵列内,其中y你以后可以随意操纵。

+0

假设我想分析问罢工价格只是在罢工价格之前我需要在代码中做出什么变化。问候 –

+0

我试图做出这样的改变,但它不工作strikePrice [row-2] = $(currentTR).find(“td:eq(10)”)。 –

+1

什么不工作? ** [这里](http://fabriciosantos.info/playground/test.php)**它工作正常。但是当然有一些细胞有一个破折号“ - ”,我想这意味着它是零。 – Fabricio