Solved

Scrape currency data from a .js file using php to enter into database

Posted on 2010-11-14
2
567 Views
Last Modified: 2012-06-27
Hi

I have been looking into cURL to understand how to use this to grab web pages, but I have become stuck trying to get the specific information I require.

I have looked at simpleXML, PHP Implode & Explode as well as Regex but am struggling to get my head around how to go about this.

I need a certain amount of currencies and their exchange rates, rather than collecting data from multiple sources I was hoping to find one source and have come across:

http://www.bloomberg.com/javascripts/currdata.js

I would assume 'cur' to be 1 USD, but I am unsure how to get the data seperated as I require so I can enter it into the database in fields such as Currency - Rate.

There will be other fields but I will look onto gathering that data when I can grasp how to get this initial problem sorted.

If you have other sources which are preferable then I am open to suggestions (Xe seems to list the currencies as well but this looks like it would involve some kind of script to interact with http://www.xe.com/ucc/convert.cgi? to get rates one by one - please correct me if I am wrong) but if you could help lead me to understanding how to gather this information to enter into database as required I would really appreciate it.



0
Comment
Question by:dchid
2 Comments
 
LVL 2

Accepted Solution

by:
kambiz earned 500 total points
ID: 34132104
Here is PHP code to extract exchange rates from the website and put them in to an associative array.

// fetch data  
$exchange_data = file_get_contents('http://www.bloomberg.com/javascripts/currdata.js');
// apply regular expression
preg_match_all("/\['(\w{3}):CUR'\]\s*=\s*(\d+(.\d+)?);/", $exchange_data, $matches);
// create array of CURRENCY => RATE
$exchange_rates = array_combine($matches[1], $matches[2]);
0
 

Author Comment

by:dchid
ID: 34140702
Perfect, exactly what I was after, thank you

If you are interested I have another regex question open here :)

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_26616715.html

If not, thank you again for the help with this one
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
t-sql split string into multiple rows 7 81
Regex HELP 2 22
AWS EC2 & RDS Instance 5 34
Which userID to use for shopping cart application 13 18
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now