[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2010-11-14
2
Medium Priority
?
576 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

650 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