username = $username; $this->password = $password; } public function api_username($username) { $this->username = $username; } public function api_password($password) { $this->password = $password; } /** * query() * * Makes curl connection to API * * $command string command to send * $req string GET|POST|DELETE request to send API * $params array POST data if request is POST * * returns raw http data (JSON object in most API cases) on success, false otherwise */ protected function query($command, $req="GET", $params = "") { $ch = curl_init(); if (substr($command, 0, strlen(API_URL)-1) == API_URL) { $URL = $command; } else { $URL = API_URL . "/" . $command; } curl_setopt($ch, CURLOPT_URL, $URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERAGENT, "Services_WTF_Test.php"); curl_setopt($ch, CURLOPT_USERPWD, $this->username.":".$this->password); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); if ($req == "POST") curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $results = curl_exec($ch); if ($results === false) $this->error = curl_error($ch); curl_close($ch); return $results; } protected function checkid() { if (empty($this->testid)) { $this->error = "No testid! Please start a new test or load an existing test first."; return false; } return true; } /** * error() * * Returns error message */ public function error() { return $this->error; } /** * test() * * Sends new test to GTMetrix API * * $data array array containing parameters to send API * * returns the testid on success, false otherwise; */ public function test($data) { if (empty($data)) { $this->error = "Parameters need to be set to start a new test!"; return false; } if (!isset($data['url']) OR empty($data['url'])) { $this->error = "No URL given!"; return false; } // check URL if (!preg_match('@^https?://@', $data['url'])) { $this->error = "Bad URL."; return false; } if (!empty($this->result)) $this->result = array(); $data = http_build_query($data); $result = $this->query("test", "POST", $data); if ($result != false) { $result = json_decode($result, true); if (empty($result['error'])) { $this->testid = $result['test_id']; if (isset($result['state']) AND !empty($result['state'])) $this->result = $result; return $this->testid; } else { $this->error = $result['error']; } } return false; } /** * load() * * Query an existing test from GTMetrix API * * $testid string The existing test's test ID * * testid must be valid, or else all query methods will fail */ public function load($testid) { $this->testid = $testid; if (!empty($this->result)) $this->result = array(); } /** * delete() * * Delete the test from the GTMetrix database * * Precondition: member testid is not empty * * returns message on success, false otherwise */ public function delete() { if (!$this->checkid()) return false; $command = "test/".$this->testid; $result = $this->query($command, "DELETE"); if ($result != false) { $result = json_decode($result, true); return ($result['message']) ? true : false; } return false; } /** * get_testid() * * Returns the testid, false if testid is not set */ public function get_testid(){ return ($this->testid) ? $this->testid : false; } /** * poll_state() * * polls the state of the test * * Precondition: member testid is not empty * * The class will save a copy of the state object, * which contains information such as the test results and resource urls (or nothing if an error occured) * so that additional queries to the API is not required. * * returns true on successful poll, or false on network error or no testid */ public function poll_state() { if (!$this->checkid()) return false; if (!empty($this->result)) { if ($this->result['state'] == "completed") return true; } $command = "test/".$this->testid; $result = $this->query($command); if ($result != false) { $result = json_decode($result, true); if (!empty($result['error']) AND !isset($result['state'])) { $this->error = $result['error']; return false; } $this->result = $result; if ($result['state'] == "error") $this->error = $result['error']; return true; } return false; } /** * state() * * Returns the state of the test (queued, started, completed, error) * * Precondition: member testid is not empty * * returns the state of the test, or false on networking error */ public function state() { if (!$this->checkid()) return false; if (empty($this->result)) return false; return $this->result['state']; } /** * completed() * * returns true if the test is complete, false otherwise */ public function completed() { return ($this->state() == "completed") ? true : false; } /* * get_results() * * locks and polls API until test results are received */ public function get_results() { while ($this->poll_state()) { if ($this->state() == "completed" OR $this->state() == "error") break; sleep(1); } } /** * locations() * * Returns a list of GTMetrix server locations accompanied by their location IDs * that can be used in newTest() to select a different server location for testing * * returns the location list in array format, the error message if an error occured, * or false if a query error occured. */ public function locations() { $result = $this->query("locations"); if ($result != false) { $result = json_decode($result, true); if (empty($result['error'])) { return $result; } else { $this->error = $result['error']; } } return false; } /** * results() * * Get test results * * returns the test results, or false if the test hasn't completed yet */ public function results() { if (!$this->completed()) return false; return $this->result['results']; } /** * resources() * * Get test resource URLs * * returns the test resources, or false if the test hasn't completed yet */ public function resources($item="all") { if (!$this->completed()) return false; return $this->result['resources']; } } ?>