diff --git a/Changelog b/Changelog index 0ea4a7fb6..256d2cc4a 100755 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +2017-01-13 DariusIIi + * Chg: Use guzzlehttp to retrieve data from ADE and ADM 2017-01-12 DariusIIi * Chg: Use guzzlehttp to retrieve data from AEBN 2017-01-11 DariusIII diff --git a/nntmux/ADE.php b/nntmux/ADE.php index 8e1205f5f..1cc8a46af 100755 --- a/nntmux/ADE.php +++ b/nntmux/ADE.php @@ -1,6 +1,9 @@ _html = new \simple_html_dom(); $this->_edithtml = new \simple_html_dom(); + $this->client = new Client(); + $this->pdo = new DB(); } /** @@ -327,25 +342,41 @@ class ADE private function getUrl($trailing = "") { if (!empty($trailing)) { - $this->_ch = curl_init(self::ADE . $trailing); + try { + $this->_response = $this->client->get(self::ADE . $trailing)->getBody()->getContents(); + } catch (RequestException $e) { + if ($e->hasResponse()) { + if($e->getCode() === 404) { + $this->pdo->log->doEcho($this->pdo->log->notice('Data not available on server')); + } else if ($e->getCode() === 503) { + $this->pdo->log->doEcho($this->pdo->log->notice('Service unavailable')); + } else { + $this->pdo->log->doEcho($this->pdo->log->notice('Unable to fetch data, http error reported: ' . $e->getCode())); + } + } + } } if (!empty($this->directLink)) { - $this->_ch = curl_init($this->directLink); - $this->directLink = ""; + try { + $this->_response = $this->client->get($this->directLink)->getBody()->getContents(); + $this->directLink = ""; + } catch (RequestException $e) { + if ($e->hasResponse()) { + if($e->getCode() === 404) { + $this->pdo->log->doEcho($this->pdo->log->notice('Data not available on server')); + } else if ($e->getCode() === 503) { + $this->pdo->log->doEcho($this->pdo->log->notice('Service unavailable')); + } else { + $this->pdo->log->doEcho($this->pdo->log->notice('Unable to fetch data, http error reported: ' . $e->getCode())); + } + } + } } - curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($this->_ch, CURLOPT_HEADER, 0); - curl_setopt($this->_ch, CURLOPT_VERBOSE, 0); - curl_setopt($this->_ch, CURLOPT_USERAGENT, "Firefox/2.0.0.1"); - curl_setopt($this->_ch, CURLOPT_FAILONERROR, 1); - curl_setopt_array($this->_ch, Utility::curlSslContextOptions()); - $this->_response = curl_exec($this->_ch); - if (!$this->_response) { - curl_close($this->_ch); + if (!$this->_response) { return false; } - curl_close($this->_ch); + return true; } diff --git a/nntmux/ADM.php b/nntmux/ADM.php index 908356953..376b5dabd 100755 --- a/nntmux/ADM.php +++ b/nntmux/ADM.php @@ -1,6 +1,11 @@ _html = new \simple_html_dom(); + $this->client = new Client(); + $this->cookiejar = new CookieJar(); + $this->pdo = new DB(); if (isset($this->cookie)) { - $this->getUrl(); + $cookieJar = $this->cookiejar->setCookie(SetCookie::fromString($this->cookie)); + $this->client = new Client(['cookies' => $cookieJar]); } } @@ -277,45 +296,44 @@ class ADM /** * Get Raw html of webpage * - * @param bool $usepost - * * @return bool */ - private function getUrl($usepost = false) + private function getUrl() { if (isset($this->_trailUrl)) { - $ch = curl_init(self::ADMURL . $this->_trailUrl); + try { + $this->_response = $this->client->get(self::ADMURL . $this->_trailUrl)->getBody()->getContents(); + } catch (RequestException $e) { + if ($e->hasResponse()) { + if($e->getCode() === 404) { + $this->pdo->log->doEcho($this->pdo->log->notice('Data not available on server')); + } else if ($e->getCode() === 503) { + $this->pdo->log->doEcho($this->pdo->log->notice('Service unavailable')); + } else { + $this->pdo->log->doEcho($this->pdo->log->notice('Unable to fetch data, http error reported: ' . $e->getCode())); + } + } + } } else { - $ch = curl_init(self::IF18); + try { + $this->_response = $this->client->get(self::IF18)->getBody()->getContents(); + } catch (RequestException $e) { + if ($e->hasResponse()) { + if($e->getCode() === 404) { + $this->pdo->log->doEcho($this->pdo->log->notice('Data not available on server')); + } else if ($e->getCode() === 503) { + $this->pdo->log->doEcho($this->pdo->log->notice('Service unavailable')); + } else { + $this->pdo->log->doEcho($this->pdo->log->notice('Unable to fetch data, http error reported: ' . $e->getCode())); + } + } + } } - if ($usepost === true) { - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postParams); - } - - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_VERBOSE, 0); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_USERAGENT, "Firefox/2.0.0.1"); - curl_setopt($ch, CURLOPT_FAILONERROR, 1); - - if (isset($this->cookie)) { - curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); - curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); - } - - curl_setopt_array($ch, Utility::curlSslContextOptions()); - $this->_response = curl_exec($ch); - if (!$this->_response) { - curl_close($ch); return false; } - curl_close($ch); $this->_html->load($this->_response); return true; }