forked from scheb/yahoo-finance-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiClient.php
157 lines (131 loc) · 4.14 KB
/
ApiClient.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Scheb\YahooFinanceApi;
use Scheb\YahooFinanceApi\Exception\HttpException;
use Scheb\YahooFinanceApi\Exception\ApiException;
class ApiClient
{
/**
* @var int $timeout
*/
private $timeout;
/**
* ApiClient constructor.
* @param int $timeout
*/
public function __construct($timeout = 5)
{
$this->timeout = $timeout;
}
/**
* Search for stocks
* @param string $searchTerm
* @return array
* @throws \Scheb\YahooFinanceApi\Exception\ApiException
*/
public function search($searchTerm)
{
$url = "http://autoc.finance.yahoo.com/autoc?query=".urlencode($searchTerm)."&callback=YAHOO.Finance.SymbolSuggest.ssCallback";
try
{
$client = new HttpClient($url, $this->timeout);
$response = $client->execute();
}
catch (HttpException $e)
{
throw new ApiException("Yahoo Search API is not available.", ApiException::UNAVIALABLE, $e);
}
//Remove callback function from response
$response = preg_replace("#^YAHOO\\.Finance\\.SymbolSuggest\\.ssCallback\\((.*)\\)$#", "$1", $response);
$decoded = json_decode($response, true);
if (!isset($decoded['ResultSet']['Result']))
{
throw new ApiException("Yahoo Search API returned an invalid result.", ApiException::INVALID_RESULT);
}
return $decoded;
}
/**
* Get historical data for a symbol
*
* @param string $symbol
* @param \DateTime $startDate
* @param \DateTime $endDate
* @return array
* @throws \Scheb\YahooFinanceApi\Exception\ApiException
*/
public function getHistoricalData($symbol, \DateTime $startDate, \DateTime $endDate)
{
$query = "select * from yahoo.finance.historicaldata where startDate='".$startDate->format("Y-m-d")."' and endDate='".$endDate->format("Y-m-d")."' and symbol='".$symbol."'";
return $this->execQuery($query);
}
/**
* Get quotes for one or multiple symbols
*
* @param array|string $symbols
* @return array
* @throws \Scheb\YahooFinanceApi\Exception\ApiException
*/
public function getQuotes($symbols)
{
if (is_string($symbols))
{
$symbols = array($symbols);
}
$query = "select * from yahoo.finance.quotes where symbol in ('".implode("','", $symbols)."')";
return $this->execQuery($query);
}
/**
* Get quotes list for one or multiple symbols
*
* @param array|string $symbols
* @return array
* @throws \Scheb\YahooFinanceApi\Exception\ApiException
*/
public function getQuotesList($symbols)
{
if (is_string($symbols))
{
$symbols = array($symbols);
}
$query = "select * from yahoo.finance.quoteslist where symbol in ('".implode("','", $symbols)."')";
return $this->execQuery($query);
}
/**
* Execute the query
* @param string $query
* @return array
* @throws \Scheb\YahooFinanceApi\Exception\ApiException
*/
private function execQuery($query)
{
try
{
$url = $this->createUrl($query);
$client = new HttpClient($url, $this->timeout);
$response = $client->execute();
}
catch (HttpException $e)
{
throw new ApiException("Yahoo Finance API is not available.", ApiException::UNAVIALABLE, $e);
}
$decoded = json_decode($response, true);
if (!isset($decoded['query']['results']) || count($decoded['query']['results']) === 0)
{
throw new ApiException("Yahoo Finance API did not return a result.", ApiException::EMPTY_RESULT);
}
return $decoded;
}
/**
* Create the URL to call
* @param array $query
* @return string
*/
private function createUrl($query)
{
$params = array(
'env' => "http://datatables.org/alltables.env",
'format' => "json",
'q' => $query,
);
return "http://query.yahooapis.com/v1/public/yql?".http_build_query($params);
}
}