0

I'm trying to get some exchange rates from another website, I'm logging in and grabbing all the data with file_get_contents, this is what I use:

<?php
$username = 'myusername@gmail.com';
$password = 'mypassword';
$url = 'http://website-i-get-content-from.com';
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context)

?>

Now I only need some parts of that website: the exchange rates for EUR CHF and GBP, in the source code it looks like this:

<tr><td>EUR</td><td align=right>USD 0.599</td><td align=right>USD 0.599</td></tr>

    <tr><td>CHF</td><td align=right>USD 0.470</td><td align=right>USD 0.470</td></tr>

    <tr><td>GBP</td><td align=right>USD 0.675</td><td align=right>USD 0.675</td></tr>

So 0.599, 0.470 and 0.675 are the numbers I need at this time. They do change obviously.

How do I put them into variables ?

user990767
  • 999
  • 1
  • 9
  • 15
  • possible duplicate of [HTML Scraping in Php](http://stackoverflow.com/questions/34120/html-scraping-in-php) – GordonM Mar 13 '12 at 08:53

2 Answers2

3

Sounds like you need a parser. I've used simpledom parser in the past. I found it quite straightforward.

include("simplehtmldom/simple_html_dom.php");

$data="<html>
<body>
<table class=\"foo\">
<tr><td>EUR</td><td align=right>USD 0.599</td><td align=right>USD
0.599</td></tr>
    <tr><td>CHF</td><td align=right>USD 0.470</td><td align=right>USD
0.470</td></tr>
    <tr><td>GBP</td><td align=right>USD 0.675</td><td align=right>USD
0.675</td></tr>
</table>
</body>
</html>";

$html = new simple_html_dom();
$html->load($data);

foreach($html->find('table.foo tr') as $row) {
  $cells = $row->find('td');
  if (count($cells) >= 3) {
    $abbr=$cells[0]->innertext; // EUR, CHF etc
    $value1=$cells[1]->innertext; // USD 0.599 etc
    $value2=$cells[2]->innertext; // USD 0.599 etc
    echo "$abbr $value1 $value2\n";
  }
}
Adam
  • 35,919
  • 9
  • 100
  • 137
  • 1
    PHP has (DOMDocument)[http://php.net/manual/en/book.dom.php], does the same thing, without needing another library. – Bradmage Mar 13 '12 at 09:03
  • Adam, I appreciate you're taking the time to help, your answer makes sense to me, although the website where I take the data from contains a lot more than what you have in your $data variable. I know this is just ment to be a starting point, but could you tell me how I can extract the part which you have in your $data variable from the whole source code of the page ? – user990767 Mar 13 '12 at 09:21
0

A regular expression could do.

preg_match_all("'(EUR|CHF|GBP)(?=<).+USD(?<=>)\s+([\d.]+)(?=<)'", file_get_contents('...'), $matches));

I didn't test it though.

I know it's bad bla bla to parse HTML but it's not really parsing here.

Savageman
  • 9,257
  • 6
  • 40
  • 50