diff --git a/.gitignore b/.gitignore index 57872d0..0dca145 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +.idea diff --git a/README.md b/README.md index 53df251..41298d7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This library allows you to interact with your Sirportly data using PHP. ## Setting up a Sirportly Client ```php -$sirportly = new Sirportly('the-token','the-secret'); +$sirportly = new \Sirportly\Sirportly('the-token','the-secret'); ``` ## Creating a ticket diff --git a/composer.json b/composer.json index 9b8cea1..83b14e0 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,9 @@ ], "minimum-stability": "stable", "autoload": { - "classmap": ["class.php"] + "psr-4": { + "Sirportly\\": "src" + } }, "require": { diff --git a/src/Exceptions/SirportlyDefaultException.php b/src/Exceptions/SirportlyDefaultException.php new file mode 100644 index 0000000..0a5eac3 --- /dev/null +++ b/src/Exceptions/SirportlyDefaultException.php @@ -0,0 +1,10 @@ +url = $url; } + /** + * @param $action + * @param array $postdata + * @return mixed + * @throws SirportlyDefaultException + */ private function query($action,$postdata=array()) { $curl = curl_init(); $query_string = ""; + $url = $this->url . $action; foreach ($postdata AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; $header = array('X-Auth-Token: '.$this->token, 'X-Auth-Secret: '.$this->secret); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_URL, $this->url.$action); + curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POSTFIELDS, $query_string); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_POST, 1); $result = curl_exec($curl); + $info = curl_getinfo($curl); + + if ($result === false || !in_array($info['http_code'], [200, 201])) { + $error = "No cURL data returned for $url [http error: ". $info['http_code']. "]"; + if (curl_error($curl)) { + $error .= "\n" . curl_error($curl); + } + throw new SirportlyDefaultException($error); + } + curl_close($curl); $decode = json_decode($result,true); return $decode; } public function ticket($ticket_reference) { - return $this->query('/api/v1/tickets/ticket',array('reference' => $ticket_reference)); + return $this->query('/api/v2/tickets/ticket', array('ticket' => $ticket_reference)); } public function create_ticket($params = array()) { - return $this->query('/api/v1/tickets/submit',$params); + return $this->query('/api/v2/tickets/submit',$params); } public function post_update($params = array()) { - return $this->query('/api/v1/tickets/post_update',$params); + return $this->query('/api/v2/tickets/post_update',$params); } public function tickets($page = '1') { - return $this->query('/api/v1/tickets/all',array('page' => $page)); + return $this->query('/api/v2/tickets/all',array('page' => $page)); } public function update_ticket($params = array()) { - return $this->query('/api/v1/tickets/update',$params); + return $this->query('/api/v2/tickets/update',$params); } public function run_macro($params = array()) { - return $this->query('/api/v1/tickets/macro',$params); + return $this->query('/api/v2/tickets/macro',$params); } public function add_follow_up($params = array()) { - return $this->query('/api/v1/tickets/followup',$params); + return $this->query('/api/v2/tickets/add_followup',$params); } public function create_user($params = array()) { - return $this->query('/api/v1/users/create',$params); + return $this->query('/api/v2/users/create',$params); } public function statuses() { - return $this->query('/api/v1/objects/statuses'); + return $this->query('/api/v2/objects/statuses'); } public function priorities() { - return $this->query('/api/v1/objects/priorities'); + return $this->query('/api/v2/objects/priorities'); } public function teams() { - return $this->query('/api/v1/objects/teams'); + return $this->query('/api/v2/objects/teams'); } public function brands() { - return $this->query('/api/v1/objects/brands'); + return $this->query('/api/v2/objects/brands'); } public function departments() { - return $this->query('/api/v1/objects/departments'); + return $this->query('/api/v2/objects/departments'); } public function escalation_paths() { - return $this->query('/api/v1/objects/escalation_paths'); + return $this->query('/api/v2/objects/escalation_paths'); } public function slas() { - return $this->query('/api/v1/objects/slas'); + return $this->query('/api/v2/objects/slas'); } public function filters() { - return $this->query('/api/v1/objects/filters'); + return $this->query('/api/v2/objects/filters'); } public function spql($params = array()) { - return $this->query('/api/v1/tickets/spql',$params); + return $this->query('/api/v2/tickets/spql',$params); } /** @@ -107,7 +130,7 @@ public function spql($params = array()) { * @return array list of knowledgebases in an array format. */ public function kb_list() { - return $this->query('/api/v1/knowledge/list'); + return $this->query('/api/v2/knowledge/list'); } /** @@ -116,7 +139,7 @@ public function kb_list() { * @return array The knowledgebase as an array. */ public function kb($kb_id) { - return $this->query('/api/v1/knowledge/tree', array('kb' => $kb_id)); + return $this->query('/api/v2/knowledge/tree', array('kb' => $kb_id)); } /**