From 593db48556a2860caf62b0f6eeaea789906f50f9 Mon Sep 17 00:00:00 2001 From: Jeremy Brown Date: Wed, 11 Nov 2020 09:17:08 -0500 Subject: [PATCH] Added exception handling when qrz or hamqth is not available/accessible. --- application/libraries/Hamqth.php | 67 +++++++++++++++------------- application/libraries/Qrz.php | 67 +++++++++++++++------------- application/models/Logbook_model.php | 52 ++++++++++----------- 3 files changed, 97 insertions(+), 89 deletions(-) diff --git a/application/libraries/Hamqth.php b/application/libraries/Hamqth.php index 69b49aad..c0ad8c86 100644 --- a/application/libraries/Hamqth.php +++ b/application/libraries/Hamqth.php @@ -56,41 +56,44 @@ class Hamqth { public function search($callsign, $key) { + $data = null; + try { + // URL to the XML Source + $xml_feed_url = 'https://www.hamqth.com/xml.php?id=' . $key . '&callsign=' . $callsign . '&prg=cloudlog'; - // URL to the XML Source - $xml_feed_url = 'https://www.hamqth.com/xml.php?id='.$key.'&callsign='.$callsign.'&prg=cloudlog'; + // CURL Functions + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $xml_feed_url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $xml = curl_exec($ch); + curl_close($ch); - // CURL Functions - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $xml_feed_url); - curl_setopt($ch, CURLOPT_HEADER, false); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $xml = curl_exec($ch); - curl_close($ch); + // Create XML object + $xml = simplexml_load_string($xml); + if (empty($xml)) return; - // Create XML object - $xml = simplexml_load_string($xml); + // Return Required Fields + $data['callsign'] = (string)$xml->search->callsign; + $data['name'] = (string)$xml->search->nick; + $data['gridsquare'] = (string)$xml->search->grid; + $data['city'] = (string)$xml->search->adr_city; + $data['lat'] = (string)$xml->search->latitude; + $data['long'] = (string)$xml->search->longitude; + $data['iota'] = (string)$xml->search->iota; + $data['us_state'] = (string)$xml->search->us_state; + $data['us_county'] = (string)$xml->search->us_county; + $data['error'] = (string)$xml->session->error; - // Return Required Fields - $data['callsign'] = (string) $xml->search->callsign; - $data['name'] = (string) $xml->search->nick; - $data['gridsquare'] = (string) $xml->search->grid; - $data['city'] = (string) $xml->search->adr_city; - $data['lat'] = (string) $xml->search->latitude; - $data['long'] = (string) $xml->search->longitude; - $data['iota'] = (string) $xml->search->iota; - $data['us_state'] = (string) $xml->search->us_state; - $data['us_county'] = (string) $xml->search->us_county; - $data['error'] = (string) $xml->session->error; - - if($xml->search->country == "United States") { - $data['state'] = (string) $xml->search->us_state; - $data['us_county'] = (string) $xml->search->us_county; - } else { - $data['state'] = null; - $data['us_county'] = null; - } - - return $data; + if ($xml->search->country == "United States") { + $data['state'] = (string)$xml->search->us_state; + $data['us_county'] = (string)$xml->search->us_county; + } else { + $data['state'] = null; + $data['us_county'] = null; + } + } finally { + return $data; + } } } diff --git a/application/libraries/Qrz.php b/application/libraries/Qrz.php index 356073dc..532a893e 100755 --- a/application/libraries/Qrz.php +++ b/application/libraries/Qrz.php @@ -56,40 +56,43 @@ class Qrz { public function search($callsign, $key) { + $data = null; + try { + // URL to the XML Source + $xml_feed_url = 'http://xmldata.qrz.com/xml/current/?s=' . $key . ';callsign=' . $callsign . ''; - // URL to the XML Source - $xml_feed_url = 'http://xmldata.qrz.com/xml/current/?s='.$key.';callsign='.$callsign.''; - - // CURL Functions - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $xml_feed_url); - curl_setopt($ch, CURLOPT_HEADER, false); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $xml = curl_exec($ch); - curl_close($ch); - - // Create XML object - $xml = simplexml_load_string($xml); - - // Return Required Fields - $data['callsign'] = (string) $xml->Callsign->call; - $data['name'] = (string) $xml->Callsign->fname . ' '. (string) $xml->Callsign->name; - $data['gridsquare'] = (string) $xml->Callsign->grid; - $data['city'] = (string) $xml->Callsign->addr2; - $data['lat'] = (string) $xml->Callsign->lat; - $data['long'] = (string) $xml->Callsign->lon; - $data['iota'] = (string) $xml->Callsign->iota; - $data['qslmgr'] = (string) $xml->Callsign->qslmgr; + // CURL Functions + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $xml_feed_url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $xml = curl_exec($ch); + curl_close($ch); - if($xml->Callsign->country == "United States") { - $data['state'] = (string) $xml->Callsign->state; - $data['us_county'] = (string) $xml->Callsign->county; - } else { - $data['state'] = null; - $data['us_county'] = null; - } + // Create XML object + $xml = simplexml_load_string($xml); + if (empty($xml)) return; - - return $data; + // Return Required Fields + $data['callsign'] = (string)$xml->Callsign->call; + $data['name'] = (string)$xml->Callsign->fname . ' ' . (string)$xml->Callsign->name; + $data['gridsquare'] = (string)$xml->Callsign->grid; + $data['city'] = (string)$xml->Callsign->addr2; + $data['lat'] = (string)$xml->Callsign->lat; + $data['long'] = (string)$xml->Callsign->lon; + $data['iota'] = (string)$xml->Callsign->iota; + $data['qslmgr'] = (string)$xml->Callsign->qslmgr; + + if ($xml->Callsign->country == "United States") { + $data['state'] = (string)$xml->Callsign->state; + $data['us_county'] = (string)$xml->Callsign->county; + } else { + $data['state'] = null; + $data['us_county'] = null; + } + } finally { + + return $data; + } } } diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 907379d8..002cda23 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -2170,39 +2170,41 @@ class Logbook_model extends CI_Model { public function loadCallBook($callsign) { - if ($this->config->item('callbook') == "qrz" && $this->config->item('qrz_username') != null && $this->config->item('qrz_password') != null) - { - // Lookup using QRZ - $this->load->library('qrz'); + $callbook = null; + try { + if ($this->config->item('callbook') == "qrz" && $this->config->item('qrz_username') != null && $this->config->item('qrz_password') != null) { + // Lookup using QRZ + $this->load->library('qrz'); - if(!$this->session->userdata('qrz_session_key')) { - $qrz_session_key = $this->qrz->session($this->config->item('qrz_username'), $this->config->item('qrz_password')); - $this->session->set_userdata('qrz_session_key', $qrz_session_key); + if (!$this->session->userdata('qrz_session_key')) { + $qrz_session_key = $this->qrz->session($this->config->item('qrz_username'), $this->config->item('qrz_password')); + $this->session->set_userdata('qrz_session_key', $qrz_session_key); + } + + $callbook = $this->qrz->search($callsign, $this->session->userdata('qrz_session_key')); } - $callbook = $this->qrz->search($callsign, $this->session->userdata('qrz_session_key')); - } + if ($this->config->item('callbook') == "hamqth" && $this->config->item('hamqth_username') != null && $this->config->item('hamqth_password') != null) { + // Load the HamQTH library + $this->load->library('hamqth'); - if ($this->config->item('callbook') == "hamqth" && $this->config->item('hamqth_username') != null && $this->config->item('hamqth_password') != null) - { - // Load the HamQTH library - $this->load->library('hamqth'); + if (!$this->session->userdata('hamqth_session_key')) { + $hamqth_session_key = $this->hamqth->session($this->config->item('hamqth_username'), $this->config->item('hamqth_password')); + $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); + } - if(!$this->session->userdata('hamqth_session_key')) { - $hamqth_session_key = $this->hamqth->session($this->config->item('hamqth_username'), $this->config->item('hamqth_password')); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); - } - - $callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key')); - - // If HamQTH session has expired, start a new session and retry the search. - if($callbook['error'] == "Session does not exist or expired") { - $hamqth_session_key = $this->hamqth->session($this->config->item('hamqth_username'), $this->config->item('hamqth_password')); - $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); $callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key')); + + // If HamQTH session has expired, start a new session and retry the search. + if ($callbook['error'] == "Session does not exist or expired") { + $hamqth_session_key = $this->hamqth->session($this->config->item('hamqth_username'), $this->config->item('hamqth_password')); + $this->session->set_userdata('hamqth_session_key', $hamqth_session_key); + $callbook = $this->hamqth->search($callsign, $this->session->userdata('hamqth_session_key')); + } } + } finally { + return $callbook; } - return $callbook; } public function update_all_station_ids() {