Question

I am trying to fetch National Stock Exchange Data and create chart using Google charts. But I am clueless how to parse the data that I am getting using the api. Here's the code of api. Any help will be appreciated. Output looks like this 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);
?>
Was it helpful?

Solution

If you want to do the job properly, you should do this all on server side and for that please refer to this answer:

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

Otherwise this should solve your problem in a quicker way:

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

All values of column 12 will be placed inside a 0 index based Array which you can later manipulate at will.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top