-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes.php
More file actions
50 lines (39 loc) · 1.58 KB
/
quotes.php
File metadata and controls
50 lines (39 loc) · 1.58 KB
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
<?php
//if valid parameters are passed to the script
if (isset($_GET['q']) && !empty($_GET['q'])) {
//prepare YQL query statement
$base_url = "https://query.yahooapis.com/v1/public/yql?q=";
$criteria = "'".str_replace(",","','",$_GET['q'])."'";
$result_fields = 'symbol, Name, LastTradePriceOnly, PercentChange, PreviousClose, MarketCapitalization';
// Form YQL query and build URI to YQL Web service
$yql_query = "select ".$result_fields." from yahoo.finance.quotes where symbol in (".$criteria.")";
$yql_query_url = $base_url . urlencode($yql_query) . "&env=store://datatables.org/alltableswithkeys&format=json";
//turn off warnings if there is latency in the YAHOO API or obscure parameters are entered then try to get data
$current_error_reporting = error_reporting();
error_reporting(0);
// Make cURL call to pull JSON data
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//$json = strip_tags(curl_exec($session));
$json = curl_exec($session);
// Inspect during creation
if (isset($_GET['format'])) {
echo "<pre>";
if ($_GET['format'] == "array"){
var_dump(json_decode(curl_exec($session), true));
} else if ($_GET['format'] == "string") {
var_dump($json);
}
echo "</pre>";
}
//echo "<pre>";
//var_dump(json_decode(curl_exec($session), true));
//var_dump($json);
//echo "</pre>";
//set error reporting to original state
error_reporting($current_error_reporting);
//return output of script
if (!isset($_GET['format']))
echo $json;
}
?>