?
Solved

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

Posted on 2010-11-14
2
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
kambiz earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

743 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