PHP : Convert CSV to JSON with YQL example
Just a note for myself for future reference. Putting this down here...maybe you may find it useful too.
This tutorial will cover how to download CSV data from Yahoo Finance and then convert the CSV data to JSON response in PHP.
The URL for downloading CSV data is
http://download.finance.yahoo.com/d/quotes.csv?s=%5EIXIC&f=sl1d1t1c1ohgv&e=.csv
You can get the URL from http://finance.yahoo.com/q?s=%5EIXIC and see the bottom-right corner - under tool box.
Next, visit Yahoo Developer and setup the query at
https://developer.yahoo.com/yql
Once you configured your query, you can copy the select statement from YQL and use it in your PHP source code. Use
file_get_contents()
andjson_decode()
functions to convert the CSV data into JSON response.
For example :
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$YQL_QUERY = "select * from csv where url='http://download.finance.yahoo.com/d/quotes.csv?s=%5EIXIC&f=sl1d1t1c1ohgv&e=.csv' and columns='symbol,price,date,time,change,col1,high,low,col2'";
$YQL = $BASE_URL . "?q=" . urlencode($YQL_QUERY) . "&format=json";
$data = file_get_contents($YQL);
$decoded = json_decode($data);
//echo "<pre>";
//print_r($decoded);
//echo "</pre>";
$last_price = $decoded->query->results->row->price;
$change = $decoded->query->results->row->change;
$high = $decoded->query->results->row->high;
$low = $decoded->query->results->row->low;
NOTE : Rate Limits
Use of the YQL should not exceed reasonable request volume. Access is limited as below:
Per application limit (identified by your Access Key): 100,000 calls per day
Per IP limits: /v1/public/*: 2,000 calls per hour; /v1/yql/*: 20,000 calls per hour
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+8.3k Golang : Another camera capture GUI application with GTK and OpenCV
+8.7k Golang : Build and compile multiple source files
+13.6k Golang : Human readable time elapsed format such as 5 days ago
+10k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+14.1k Golang : Parsing or breaking down URL
+13.1k Golang : Generate Code128 barcode
+18.1k Golang : Logging with logrus
+10.2k Golang : Create matrix with Gonum Matrix package example
+24.9k Golang : Storing cookies in http.CookieJar example
+9.4k Golang : Detect number of active displays and the display's resolution
+16.4k Golang : Gzip file example