Solved

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

Posted on 2010-11-14
2
566 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change password links 7 72
Echo images using file system 2 30
xampp tool 12 24
How to loop through records  using SELET COUNT 2 14
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 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