0

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);
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Abhishek Kumar
  • 2,136
  • 3
  • 24
  • 36

1 Answers1

0

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.

Community
  • 1
  • 1
Fabricio
  • 839
  • 9
  • 17
  • suppose i want parse Ask Qty column which is just before Strike Price what changes i need to make in the code. Regards – Abhishek Kumar Feb 13 '14 at 05:25
  • i tried to make changes like this but its not working strikePrice[row-2] = $(currentTR).find("td:eq( 10 )").text(); – Abhishek Kumar Feb 13 '14 at 05:31
  • 1
    What exactly doesn't work? **[Here](http://fabriciosantos.info/playground/test.php)** it works fine. But of course there are some cells who have a dash "-" which I suppose means it's a zero. – Fabricio Feb 13 '14 at 07:18
  • yes so i want to get 0 instead of those dash in the alert being displayed – Abhishek Kumar Feb 13 '14 at 10:09
  • And i complete agree that you have been very helpful – Abhishek Kumar Feb 13 '14 at 10:09
  • Mate... You are lazying now... :-) All you need to do is check the value and if it is equal to `-` then store `0` otherwise store the value. Anyway, the **[page is changed](http://fabriciosantos.info/playground/test.php)** to accommodate the need. Please mind that I used the `Net Chng` column because the `Ask Qty` had no `-` today. – Fabricio Feb 13 '14 at 10:44
  • i am so sorry for that i just implemented it. Thank you once again for the help – Abhishek Kumar Feb 13 '14 at 10:51