www.webdeveloper.com
Results 1 to 2 of 2

Thread: Open Exhange rates

Hybrid View

  1. #1
    Join Date
    Dec 2013
    Posts
    3

    Open Exhange rates

    I trying to create a currency converter for my web site based on this example:

    http://dynamicguru.com/currency-converter/

    This example is pulling the exchange rate data from an API. Documentation for the API can be found here:

    https://openexchangerates.org/documentation.

    If you view the source code for the example and click on link for "files/js/script.js.php" You can see that it has JSON data being retrieved from the API. I don't understand how this example is doing this. I assume is it doing it server side using PHP because of the .php at the end of the file name.

    By looking at the documentation provided by the API I can see that I can use the code below to retrieve the data but I am unsure how to assign it to the 'data' variable so that it the data displays like it does in the example.

    The data being retrieved comes from this source:

    http://openexchangerates.org/api/lat...40f3aeb72e5c36

    On the documentation it recommends doing something like this:

    Code:
    // Use jQuery.ajax to get the latest exchange rates, with JSONP:
    $.ajax({
        url: 'http://openexchangerates.org/api/latest.json?app_id=YOUR_APP_ID',
        dataType: 'jsonp',
        success: function(json) {
            // Rates are in `json.rates`
            // Base currency (USD) is `json.base`
            // UNIX Timestamp when rates were collected is in `json.timestamp`
    
        }
    
    });
    So basically I need to edit the code above to retrieve the data from the source and assign it variable called 'data' so that it displays just like in the example. Now this is probably quite simple to do but my experiences with web development is limited and I have been looking at this for around five hours straight now so I sorry if this is a silly post but I'm desperate for the help, thanks.

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    I didn't see any link on the documentation website that mentioned anything about files/js/script.js.php, however, there was an example using PHP, so I assume that's what you meant. In that case, they're initializing a new cURL session, retrieving data from the specified URL and storing it as a String variable ($json). After that, it's parsing the JSON and outputting the results.

    In the jQuery example, you would have to pass in your app ID. You can store the results from the JSON response in separate variables, such as:

    Code:
    $.ajax({
        url: 'http://openexchangerates.org/api/latest.json?app_id=YOUR_APP_ID',
        dataType: 'jsonp',
        success: function(json) {
            var $data = json.rates + "@" + json.base;
        }
    
    });
    I used the "@" as a delimiter so you will be able to easily parse the results and display them in separate columns, similar to the example you gave. Alternatively, you can assign them to different variables from the start as I'm not sure why you have to put them all in 1 variable.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles