Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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 count occurrences of each item in an array.

706 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

18 Experts available now in Live!

Get 1:1 Help Now