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