0
votes

I found/hacked (and I do mean hacked, I know it's not pretty) the code below.

What I am to accomplish:

  • Get both the ask/bid value for 3 currencies
  • Currencies are USD, BRL,
  • EUR Base currency is ARS
  • Avoid getting too fancy, I just need to echo 6 values (bid/ask for each currency from the base currency)

I'm able to get what I think is the average rate, but I'm unsure how to get the bid/ask values.

I do notice that: - if you change $usd_allData[1] to $usd_allData[2], you get the date - if you change $usd_allData[1] to $usd_allData[3], you get the time

If you have any insights or flashes of genius, please send them my way.

Thanks in advance!


<?php

/* USD
------------------------- */
$usd_from   = 'USD'; /*change it to your required currencies */
$usd_to     = 'ARS';
$usd_url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $usd_from . $usd_to .'=X';
$usd_handle = @fopen($usd_url, 'r');

if ($usd_handle) {
    $usd_result = fgets($usd_handle, 4096);
    fclose($usd_handle);
}
$usd_allData = explode(',',$usd_result); /* Get all the contents to an array */
$usd_Value = $usd_allData[1];

/* EUR
------------------------- */
$eur_from   = 'EUR'; /*change it to your required currencies */
$eur_to     = 'ARS';
$eur_url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $eur_from . $eur_to .'=X';
$eur_handle = @fopen($eur_url, 'r');

if ($eur_handle) {
    $eur_result = fgets($eur_handle, 4096);
    fclose($eur_handle);
}
$eur_allData = explode(',',$eur_result); /* Get all the contents to an array */
$eur_Value = $eur_allData[1];

/* BRL
------------------------- */
$brl_from   = 'BRL'; /*change it to your required currencies */
$brl_to     = 'ARS';
$brl_url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $brl_from .     $brl_to .'=X';
$brl_handle = @fopen($brl_url, 'r');

if ($brl_handle) {
    $brl_result = fgets($brl_handle, 4096);
    fclose($brl_handle);
}
$brl_allData = explode(',',$brl_result); /* Get all the contents to an array */
$brl_Value = $brl_allData[1];

echo (
    $usd_Value . '<br><hr>' .
    $eur_Value . '<br><hr>' .
    $brl_Value 
)

?>
2

2 Answers

1
votes

Have you thought about Google instead of Yahoo? Need API for currency converting

I'd also just print_r() your arrays to see what comes out instead of picking at it item by item.

0
votes

You just made my day. Doing a 'print_'r of the array and then a quick Google Search led me to this article titled 'Yahoo Finance (hidden) API'

Turns out that this bit is the key:

http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=

Specifically, what follows this:

&f=

The letters that follow this get the respective variable. So, this gets sl1, d1, t1.

sl1d1t1

Changing that to the bit below (b2, b3), gets the real-time bid and ask pice (how accurate it is, I've no idea)

sl1d1t1b2b3

Huge, huge, huge thanks!