forked from m140v/Real-time-Stock-Price-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
125 lines (85 loc) · 2.95 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, Version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
//author: Mike De'Shazer || @itsdeshazer
// Start by getting the ticker symbol and assigning it to variable $ticker
extract($_REQUEST);
if(isset($ticker)){
//check if $ticker is set
$ticker = strtoupper($ticker);
//URLs (valid on 4/8/2012)
//These need to be changed as the formats of the pages change over time
$reuters= "http://reuters.com/finance/stocks/overview?symbol=". $ticker;
$nasdaq= "http://www.nasdaq.com/symbol/". $ticker;
$yahoo= "http://finance.yahoo.com/q?s=". $ticker;
//Try to retrieve Reuters price first (most reliable of 3 possible sources)
$reutResult = file_get_contents($reuters);
$nyArr1 = explode( 'font-size: 23px;">', $reutResult);
if($nyArr1[1]){
$nyArr2 = explode( "</span>", $nyArr1[1]);
if($nyArr2[1]){
$nyPrice = $nyArr2[0];
}
}
if($nyPrice){
// We have Reuter's price data for this stock
$jsonResponse = '{"price": "'.floatval($nyPrice).'", "source": "Reuters"}';
echo json_encode($jsonResponse);
return;
}
else{
//could not get Reuters, so trying Nasdaq
$nasResult = file_get_contents($nasdaq);
//Try to retrieve Nasdaq price:
$nasArr1 = explode( "_LastSale1'>", $nasResult);
if($nasArr1[1]){
$nasArr2 = explode( "</label>", $nasArr1[1]);
if($nasArr2[1]){
$nasPrice = $nasArr2[0];
}
}
if($nasPrice){
//we have Nasdaq's price
$nasPrice = str_replace("$", "", $nasPrice);
$nasPrice = str_replace(" ", "", $nasPrice);
$jsonResponse = '{"price": "'. $nasPrice.'", "source": "Nasdaq"}';
echo json_encode($jsonResponse);
//return;
}
else{
//could not get Nasdaq or Reutors, so trying Yahoo
$yahResult = file_get_contents($yahoo);
$ticker = strtolower($ticker);
$yahArr1 = explode( 'id="yfs_l84_'.$ticker.'">', $yahResult);
if($yahArr1[1]){
// echo $yahArr1[1];
$yahArr2 = explode( " ", $yahArr1[1]);
if($yahArr2[1]){
$yahPrice = $yahArr2[0];
}
}
if($yahPrice){
$jsonResponse = '{"price": "'.floatval($yahPrice).'" , "source": "Yahoo"}';
echo json_encode($jsonResponse);
//return;
}
else{
$jsonResponse = '{"error": "Y"Please make sure you passed a valid stock sticker symbol. (e.g. yoursite.com/?ticker=GOOG). If this error persists, please update this script with the latest version ( https://github.com/m140v/Real-time-Stock-Price-API/). The source site might have been reformatted."}';
echo json_encode($jsonResponse);
return;
}
}
}
}
else{
$jsonResponse = '{"error": "please send a ticker symbol in your request with the key `ticker`."}';
echo json_encode($jsonResponse);
return;
}
?>