Good day my fellow Bitcoiners. I figured I should share a simple little script I made for fetching the Mt. Gox ticker data using PHP. I'll also teach you how to automate it using Cron Jobs. I hope this will be useful to some of you.
You should not use a script that sends a request to Mt. Gox every time the page is loaded. That is very bad practice because it puts unnecessary strain on Mt. Gox and your pages will take longer to load all the time.
The solution is to create a stand-alone script that fetches the ticker data and saves it to a file and/or into a database. You then set up something like Cron Jobs to fire the script at your preferred intervals of time.
One hour is about the minimum amount of time you should wait before each request. The script uses the PHP cURL function to retrieve the ticker data and then simply writes it to a file. Saving the data to a database is optional.
If you wish to know more about the script you can simply download it and check the source out yourself (see bottom of post for download). Now lets move on to automation, which is where some people can get stuck.
If you have cPanel you can easily set up Cron Jobs to automate a task. The time stuff is pretty self-explanatory, it dictates the period between each time the script will run. The command part is where you need to input the following:php -q /home/serverpath/goxgrab/mtgox_update.php
All you need to do is change the first part "/home/serverpath/" to the correct location to your "goxgrab" folder (after you upload it; you might want to install the script before setting up Cron Jobs - see the install.txt file), then submit the Cron Job.
The included mtgox_stats.php file shows you how to extract the ticker data from your local copy and save it as an associative array. This file can easily be included into other scripts that need access to the Mt. Gox ticker data.
Download the project from bitfreak.co.cc: GoxGrab
All feedback is welcome, especially about bugs or suggestions. Donations are also appreciated.
NEXT TIME: Using HTML5 to graph ticker data!
EDIT: Just did some heavy updates. If you have downloaded it already, I suggest you do it again, unless you already figured out how to make the mtgox_stats.php file work properly.