2 Replies - 10891 Views - Last Post: 08 February 2012 - 06:34 PM Rate Topic: -----

#1 NewToJava2011   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Unable to pass Google currency API into file_get_contents()

Posted 08 February 2012 - 01:46 PM

I'm creating a single configuration file which holds the URL's for various API's and RSS feeds. For some reason I'm unable to pass Google currency API URL into my file_get_contents() method.

I store the URL in my single configuration file; cityConfig.php script
<?php

// Feed URL's //
$googleCurrencyApi = 'http://www.google.com/ig/calculator?h1=en&q='; 


?>

The first function in calculator.php script adds arguments onto the API URL after a form submission.
<?php

require_once 'cityConfig.php';

function currency_convert($amount, $from, $to) {
    $result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to);
    $expl = explode('"', $result);

    if ($expl[1] == '' || $expl[3] == '') {
        return false;
    } else {
        return array(
            $expl[1],
            $expl[3]
        );
    }
}
?>


The above function errors with: Warning: file_get_contents(10gbp=?gbp) [function.file-get-contents]: failed to open stream:

if (isset($_POST['amount'], $_POST['from'], $_POST['to'])) {
    $amount = trim($_POST['amount']);
    $from = $_POST['from'];
    $to = $_POST['to'];

    $conversion = currency_convert($amount, $from, $to);
    if ($conversion == false) {
        echo 'Sorry, something went wrong';
    } else {
        echo number_format((float) $conversion[0], 2) . ' ' . strtoupper($to), ' = ', number_format((float) $conversion[1], 2) . ' ' . strtoupper($to);
    }
}
?>


However, if I replace the $googleCurrencyApi variable and hard code the URL into the function it works fine. Any ideas why I can't pass in the $googleCurrencyApi variable?

This post has been edited by NewToJava2011: 08 February 2012 - 01:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to pass Google currency API into file_get_contents()

#2 NewToJava2011   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Re: Unable to pass Google currency API into file_get_contents()

Posted 08 February 2012 - 01:57 PM

Need to parse in the $googleCurrencyApi into the method

function currency_convert($googleCurrencyApi, $amount, $from, $to) {
    $result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to);
    $expl = explode('"', $result);

    if ($expl[1] == '' || $expl[3] == '') {
        return false;
    } else {
        return array(
            $expl[1],
            $expl[3]
        );
    }
}
?>

Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Unable to pass Google currency API into file_get_contents()

Posted 08 February 2012 - 06:34 PM

I already explained this concept to you in detail in one of your other threads:
Problem passing a second parameter into a PHP function
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1