Jump to content

Returning Results from YQL currency exchange database!


will_1990

Recommended Posts

Hi All,

 

Its been a while since i've posted, its good to be back.

 

I am currently trying to pull two exchange rates against the $ using YQL's restful url. Unfortunately I am having a huge problem returning the results correct.... currently my array will only output  a single rate (instead of two) and the hash array returns with the base rate name rather than the expected currency code.

 

I hope someone can help me sort this as it's given me quite a headache!

 

Please see the code below:

 

<?php
require_once('uwe_proxy.php');

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

# use the yahoo YQL rest service to return any number of rates in a hash 
function get_yahoo_rest_rate ($base, $curr_arr) { 
    
    $params = ''; 
    $rates = array(); 
     
    # define the YQL rest url head & tail 
    $yql_base_uri = "http://query.yahooapis.com/v1/public/yql"; 
    $yql_tail_uri = '&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'; 
     
    # build up the $params value 
    foreach($curr_arr as $curr) { 
        $params .= $curr.$base.','; 
    } 
         
    #strip last comma 
    $params = substr($params, 0, -1); 
     
    # build the query with the params as comma sep. string eg. USDGBP,USDBOB,.. 
    $yql_query = "select * from yahoo.finance.xchange where pair IN ('{$params}')"; 
     
    # build the the complete YQL query 
    $yql_query_url = $yql_base_uri . "?q=" . urlencode($yql_query) . $yql_tail_uri; 
     
    # send the query via proxy and get as string and load as simplexml object 
    $yql_response = @simplexml_load_string(file_get_contents_proxy($yql_query_url)); 
    Print_r($yql_query_url); //- debugging only
    # process simplexml object and return a sorted hash of rates or FALSE on error 
    if ($yql_response) {
        
        foreach($yql_response->results->rate as $rate) { 
            if ((float)$rate->Rate>0) { 
                $rates[substr((string)$rate->attributes()->id, -3)] = (float)$rate->Rate; 
             
        } }
        ksort($rates); 
        return $rates; 
    } 
    else { 
        return FALSE; 
    }// print_r($yql_response); - debugging only
}


/////// - debugging only
$curr_arr = array('GBP','GBP');
$rates = get_yahoo_rest_rate ('USD', $curr_arr);
   //PRINT_R($yql_response);
print_r($rates);

?>

 

Sorry about dumping the whole lot - but I really don't know where this is going wrong!

 

Thanks in Advance for any help or pointers in the right direction!

 

:)

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.