public function generate() { if (!$this->__wordApiURL) { throw new PasswordGeneratorException('No URL specified. Please specify a valid URL with setWordApiURL().'); } for ($i = self::__minNumberOfWords; $i <= $this->__numberOfWords; $i++) { $wordApiInterface = new SimpleHttpGetRequest(); $wordApiInterface->setURL($this->__wordApiURL); $data = trim($wordApiInterface->get()); $status = $wordApiInterface->getStatus(); if ($status === 200) { $this->__wordList[] = $data; } else { throw new PasswordGeneratorException('Data not received! Status not 200.'); } } if ($this->__showNumber) { $this->__wordList[] = mt_rand(0, 100); } if ($this->__showSpecialChar) { $this->__wordList[] = $this->__specialChars[array_rand($this->__specialChars)]; } return implode($this->__delimeter, $this->__wordList); }
<?php require './resources/passgen.php'; define('__apiURL__', 'http://randomword.setgetgo.com/get.php'); $testAPI = function ($apiURL) { $api = new SimpleHttpGetRequest(); $api->setURL($apiURL); return $api->get() && $api->getStatus() === 200; }; $passGen; $passWord; $showApiError; if ($testAPI(__apiURL__)) { $showApiError = false; $passGen = new PasswordGenerator($_GET); $passGen->setWordApiURL(__apiURL__); $passWord = $passGen->generate(); } else { $showApiError = true; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <title>Project 2</title> <!-- Bootstrap 3.3.5 --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">