0

I have function that basically returns visitor's country code and another that gets the currency. While running XAMPP on Windows, the return values of the function gets echoed like expected. On Linux (eOS Loki), I've manually installed the LAMPP stack (not running XAMPP), this time PHP won't echo the return values of the functions. The page would stop loading, with no errors.

page stops loading immediately

NB: nhazi.dev is localhost

Country code function

function get_ipinfo( $option )
{
  $url = "http://ipinfo.io/" . $_SERVER['REMOTE_ADDR'] . "/json";


  if($_SERVER['REMOTE_ADDR'] == '127.0.0.1') // I develop offline most times.
  {
    $url = "http://nhazi.dev/php_curl/json"; //Offline Debugging
  }

  $ch = curl_init($url);
  curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true
    )
  );

  $data = json_decode( curl_exec($ch) ); // decodes json response
  curl_close($ch);

  // Return value based on option
  if(! $option)
    return $data;
  if($option == 'ip')
    return $data->ip;
  if($option == 'city')
    return $data->city;
  if($option == 'region')
    return $data->region;
  if($option == 'country')
    return $data->country;
  if($option == 'location')
    return $data->loc;
  if($option == 'org')
    return $data->org;
}

Currency code function

function get_currency( $countrycode )
{
    $url = "http://country.io/currency.json";

    if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
    {
        $url = "http://nhazi.dev/php_curl/currency"; //Offline Debugging
    }

    $ch = curl_init($url);
    curl_setopt_array($ch, array(
        CURLOPT_RETURNTRANSFER => true,
        )
    );
    $currency = json_decode(curl_exec($ch));
    // print_r($currency); // Debugging
    curl_close($ch);
    return $currency->$countrycode;
}

Function Implementation

<p>
    <?php echo get_currency( get_ipinfo('country') ); ?>
</p>
Chuma Umenze
  • 933
  • 12
  • 18
  • What is `$option`? I can't see it anywhere but at the returns – Andreas Sep 09 '17 at 20:04
  • 1
    Did you turn on PHP errors? ini_set('error_reporting', E_ALL); – Erik Baars Sep 09 '17 at 20:05
  • Turn on php errors, that's the only way to trace out – Manzoor Samad Sep 09 '17 at 20:07
  • Are you able to receive json response from the url ? – Deepansh Sachdeva Sep 09 '17 at 20:12
  • do something like $tmp =...... then var_dump($tmp); to see what is coming back - maybe it's an empty string – anmari Sep 10 '17 at 06:46
  • @Andreas $option is the specific data that should be returned. Either ip, city, region, country, location. ipinfo.io returns a json response which I've decode into $data when executing cURL. If an argument is provided which calling ipinfo(), any of the values aforementioned is returned. – Chuma Umenze Sep 10 '17 at 17:25
  • @Erik I have turned on error reporting but no errors are being displayed. The implementation does not work in eOS, but using XAMPP on Windows, it works fine and displays no errors. – Chuma Umenze Sep 10 '17 at 17:27
  • @ManzoorSamad Already turned it on but doesn't display any errors at all. error_reporting(E_ALL) shows nothing. – Chuma Umenze Sep 10 '17 at 17:28
  • @anmari nothing. Same problem the page just stops loading with no errors. – Chuma Umenze Sep 10 '17 at 17:31
  • @ChumaUmenze Check your php ini settings (echo phpinfo()), and check if curl is enabled. – Erik Baars Sep 10 '17 at 17:48
  • You are trying to do too much in one go. Break down the steps, and check at each point. Check the return of the curl exec. https://stackoverflow.com/questions/16452636/php-curl-what-does-curl-exec-return Maybe it's timing out. – anmari Sep 13 '17 at 09:53
  • I have tried all these. The issue is still not resolved – Chuma Umenze Sep 19 '17 at 02:21

0 Answers0