-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStock.php
72 lines (57 loc) · 2.07 KB
/
Stock.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
<?php
/**
* Installed resoruce for stock information using Yahoo API with YQL
*/
class Stock{
/**
* The set of REST parameters that this resource requires.
* Note that
* required parameters are passed as part of the URI, not as a query string parameter.
* you have access to all of the standard Laravel 4 classes and components (e.g. Request,...)
*
*/
public static function getParameters(){
return array(
'symbol' => array(
'required' => true,
'description' => "The official stock symbol. It's also possible to specify multiple symbols by seperating them with a plus sign.",
)
);
}
/**
* Set parameters to be used in the read function, you can manipulate or validate your REST parameters here
*/
public function setParameter($key, $value){
$this->$key = $value;
}
/**
* For semantic resources only (optional)
*/
public function getNamespaces(){
return array();
}
/**
* Return an array with your data
*/
public function getData(){
// Register new browser
$browser = new Buzz\Browser();
// Get response
$response = $browser->get("http://finance.yahoo.com/d/quotes.csv?s=" . $this->symbol . "&f=snl1c6p2ohgpv");
$data = explode("\n", $response->getContent());
$stocks = array();
// Parse CVS results
foreach($data as $line){
$line = trim($line);
if(strlen($line) > 0 && substr_count($line, ',') == 9){
$quote = array();
list($quote['symbol'], $quote['name'], $quote['value'], $quote['change'], $quote['change_percent'], $quote['open'], $quote['high'], $quote['low'], $quote['previous_close'], $quote['volume']) = explode(",", $line);
foreach ($quote as $key => $value) {
$quote[$key] = trim(preg_replace("/\"/m", "", $value));
}
array_push($stocks, $quote);
}
}
return $stocks;
}
}