解析国家证券交易所数据并创建图表

时间:2014-02-12 10:36:29

标签: php jquery parsing stock

我正在尝试获取国家证券交易所数据并使用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);
?>

1 个答案:

答案 0 :(得分: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索引的数组中,以后可以随意操作。