Brad Brad - 3 months ago 25
PHP Question

Disable errors when getting postal code using ipapi.co

I'm retrieving the postal code by using -

<?php $pstcde = file_get_contents('https://ipapi.co/'.$_SERVER['REMOTE_ADDR'] . '/postal/'); ?>


It works great but if the postal code is not found, it says, "None". I can't find a way to disable the message.

Answer

You can empty the variable if the respond is None. With a simple check.

<?php 
  $pstcde = file_get_contents('https://ipapi.co/'.$_SERVER['REMOTE_ADDR'] . '/postal/'); 
  if($pstcde === "None") 
      $pstcde = ""; //Empty string. Or you can use unset($pstcde); if you want to unset the variable.
?>
Comments