PHP Google Finance CSV

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Hey everyone,
Recently I decided to work on a personal project that compiles stock quotes from a few stock symbols(companies) from google finance. I found that you can simply go to a specific url and google will spit out a csv file.

An example of this

Now, I'm fully aware of the whole same domain policy, so what I'd like to do is simply open the file for reading, copy the contents, and write the contents to a file on my local server. That will eliminate the cross site scripting issue.

Once that's complete, I'd like to parse the values into an array, which will eventually be a php database.

Point is, I'm getting lost on parsing. I am fully capable of getting the contents of the file and saving them to a local file - not a problem. However, my issue is in parsing the data. I've never parsed csv data with php before. Anyone have an idea on how to parse the csv file and load it into an array? Or even better, convert it with php to a json format?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Do you need historical records? If not I'd forget about using a database. Depending on which version of PHP you're using, there's the str_getcsv function, or the older version compatible fgetcsv that will work with a traditional fopen file handle.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

I'll need historical records, but not for any huge length of time. Something like a year at max, but more generally between a week and 6 months depending on the symbol.

The str_getcsv looks perfect. I'll use that. Any idea on how I should structure this database? I'm pretty new to mysql and I'm getting lost with the historical data part. Not sure how I should construct the schema for this thing.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

How much control do you have over the server? Are you able to use MySQL's LOAD DATA INFILE?
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

yep, local server.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

In that case, I'd skip having PHP work with the CSV file all together. Well, other than retrieving it maybe. Fetch the CSV file, then have MySQL import that CSV file into the database via LOAD DATA INFILE.

From there, maybe have a CRON job or something export from the database to static JS files in your JSON format.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Will it work with a csv file? Or will I need to parse the data using php into a format sql will read?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Depends on the database engine. If you're using MySQL like I assumed you are, LOAD DATA INFILE is designed to work with CSV files.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 105 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.