Use guzzlehttp to retrieve data from ADE and ADM

This commit is contained in:
DariusIII
2017-01-13 10:15:18 +01:00
parent 767a494dd9
commit 7fe20057f5
3 changed files with 93 additions and 42 deletions
+2
View File
@@ -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
+44 -13
View File
@@ -1,6 +1,9 @@
<?php
namespace nntmux;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use nntmux\db\DB;
use nntmux\utility\Utility;
@@ -69,10 +72,22 @@ class ADE
protected $_edithtml;
protected $_ch;
/**
* @var Client
*/
protected $client;
/**
* @var DB
*/
protected $pdo;
public function __construct()
{
$this->_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;
}
+47 -29
View File
@@ -1,6 +1,11 @@
<?php
namespace nntmux;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\SetCookie;
use GuzzleHttp\Exception\RequestException;
use nntmux\db\DB;
use nntmux\utility\Utility;
class ADM
@@ -45,6 +50,16 @@ class ADM
*/
protected $_html;
/**
* @var Client
*/
protected $client;
/**
* @var DB
*/
protected $pdo;
/**
* POST Paramaters for getUrl Method
*/
@@ -78,8 +93,12 @@ class ADM
public function __construct()
{
$this->_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;
}