• Resolved GaryKay

    (@garykay)


    Hi,

    I need to return json values from the google currency api. I have tried using the wp_remote_get() method but nothing is returned. Not really sure what I’m doing with it. The code snippet I’m using is from https://currency-api.appspot.com/ , the php code is

    $url = "https://currency-api.appspot.com/api/USD/EUR.json?amount=1.00";
    
    $result = file_get_contents($url);
    $result = json_decode($result);
    
    if ($result->success) {
    
    	echo "1 USD is worth $result->rate EUR";
    }

    I would like to use this within a theme template page

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Joey

    (@leglesslizard)

    Hey,

    I copied and pasted your snippet above and it works fine. What it’s doing is grabbing the content of the given url which is a string of data in json format. Then you use json_decode to format the data into a php object so that it is easy to work with and if the successful parameter is set, echo out the “rate” within the string.

    This should work anywhere as long as it is inside php tags “<?php code goes here ?>” but if you wanted to check what is being returned from the url try sticking var_dump( $result ); before the if statement and seeing what is actually being retrieved.

    Hope this helps! Good luck!

    Thread Starter GaryKay

    (@garykay)

    Hi Joey,

    Thanks for the feed back. It works perfectly using localhost, as soon as I use it through an online server it does not work? Possibly a port issue? Same theme, just working locally, not too important.

    I had found a work around using ACF.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Returning API values from Google’ is closed to new replies.