PHP Google Finance CSV

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post October 10th, 2011, 1:32 pm

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?
Use your words like arrows to shoot toward your goal.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 10th, 2011, 1:32 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post October 10th, 2011, 3:41 pm

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.
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post October 10th, 2011, 4:26 pm

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.
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post October 10th, 2011, 5:57 pm

How much control do you have over the server? Are you able to use MySQL's LOAD DATA INFILE?
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post October 10th, 2011, 6:05 pm

yep, local server.
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post October 12th, 2011, 2:12 pm

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.
Strong with this one, the sudo is.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Joined: Aug 04, 2008
  • Posts: 846
  • Status: Offline

Post October 12th, 2011, 2:57 pm

Will it work with a csv file? Or will I need to parse the data using php into a format sql will read?
Use your words like arrows to shoot toward your goal.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post October 15th, 2011, 9:45 am

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.
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 199 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.