getting paris to work..
[pear] / Finance / ISIN.php
1 <?php
2 class Finance_ISIN 
3 {
4
5     var $map = array();
6
7     function getISIN($stockcode, $exchange) 
8     {
9         $ar = explode('.', $stockcode);
10
11         if(count($ar) != 2) {
12             // get isin by exchange
13             // support NYSE
14             switch($exchange) {
15                 case 'NYSE':
16                 case 'NASDAQ':
17                     return $this->getExchangeISIN($stockcode, $exchange);
18                 default:
19                     return false;
20                 
21             }
22             
23         }
24         if ($ar[1] == 'PA') {
25               return $this->getExchangeISIN($ar[0], 'EURONEXT');
26         }
27
28         // get isin by location
29
30         $file = dirname(__FILE__) . '/ISIN/' . $ar[1] . '.php';
31
32         // invalid / not supported
33         if(!file_exists($file)) {
34             return false;
35         }
36
37         require_once $file;
38
39         $cls = 'Finance_ISIN_' . $ar[1];
40
41         // invalid / not supported
42         if(!class_exists($cls)) {
43             return false;
44         }
45
46         $c = new $cls();
47         return $c->getLocationISIN($stockcode);
48     }
49
50     // data from Trading View (A more reliable source should be used if found)
51
52     function getExchangeISIN($stockcode, $exchange)
53     {
54         $ar = explode('.', $stockcode);
55
56         $ch = curl_init();
57         curl_setopt($ch, CURLOPT_URL, 'https://www.tradingview.com/symbols/' . $exchange . '-' . $stockcode .'/');
58         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
59         $str = curl_exec($ch);
60         curl_close($ch);
61
62         $matches = array();
63         preg_match('/window.initData.symbolInfo = ({.*});/', $str, $matches);
64
65         if(empty($matches)) {
66             return false;
67         }
68
69         $ret = json_decode($matches[1]);
70
71         if(!empty($ret->isin)) {
72             return $ret->isin;
73         }
74
75         return false;
76     }
77
78     function getLocationISIN($stockcode)
79     {
80         return isset($this->map[$stockcode]) ? $this->map[$stockcode] : false;
81     }
82 }