|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Trestle for PHP |
| 4 | + * |
| 5 | + * Copyright (c) 2011, Talldude Networks, LLC. All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * - Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * - Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * Trestle Class |
| 31 | + * |
| 32 | + * @link https://github.com/talldude/trestle-php-client |
| 33 | + * @version 0.0.1 |
| 34 | + */ |
| 35 | +class Trestle |
| 36 | +{ |
| 37 | + // Credentials |
| 38 | + private static $__api_key = null; // Trestle API Key |
| 39 | + private static $__api_secret = null; // Trestle API Secret |
| 40 | + |
| 41 | + // Endpoints |
| 42 | + public static $user_service_url = 'https://www.trestleapp.com/v1/user'; |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructor |
| 46 | + * |
| 47 | + * @param string $accessKey Access key |
| 48 | + * @param string $secretKey Secret key |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function __construct($api_key = null, $api_secret = null) |
| 52 | + { |
| 53 | + self::$__api_key = $api_key; |
| 54 | + self::$__api_secret = $api_secret; |
| 55 | + } |
| 56 | + |
| 57 | + //------------------------------- |
| 58 | + // USER Service |
| 59 | + //------------------------------- |
| 60 | + |
| 61 | + /** |
| 62 | + * UserCreate - create a new user in the User Service |
| 63 | + * |
| 64 | + * @param array Account Creation parameters |
| 65 | + * @return mixed array on success, error message on failure |
| 66 | + */ |
| 67 | + public function UserCreate(&$_args) |
| 68 | + { |
| 69 | + $tmp = $this->_request(self::$user_service_url,'POST',$_args); |
| 70 | + return $tmp; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * UserUpdate - update an existing user account in the User Service |
| 75 | + * |
| 76 | + * @param array Account Creation parameters |
| 77 | + * @return mixed array on success, error message on failure |
| 78 | + */ |
| 79 | + public function UserUpdate($user_id,&$_args) |
| 80 | + { |
| 81 | + return $this->_request(self::$user_service_url ."/{$user_id}",'PUT',$_args); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * UserDelete - delete an existing user from the User Service |
| 86 | + * |
| 87 | + * @param array Account Creation parameters |
| 88 | + * @return mixed array on success, error message on failure |
| 89 | + */ |
| 90 | + public function UserDelete($user_id) |
| 91 | + { |
| 92 | + return $this->_request(self::$user_service_url ."/{$user_id}",'DELETE'); |
| 93 | + } |
| 94 | + |
| 95 | + //------------------------------- |
| 96 | + // Helper functions |
| 97 | + //------------------------------- |
| 98 | + |
| 99 | + /** |
| 100 | + * _request - try/catch wrapper for _trestle_request |
| 101 | + * |
| 102 | + * @param string $message Error Message |
| 103 | + * @param string $file Error Filename |
| 104 | + * @param integer $line Error Line Number |
| 105 | + * @param integer $code Error Code |
| 106 | + * @return void |
| 107 | + */ |
| 108 | + private function _request($url,$method,&$_args = false) |
| 109 | + { |
| 110 | + try { |
| 111 | + $_json = $this->_trestle_request($url,$method,$_args); |
| 112 | + } |
| 113 | + catch (Exception $e) { |
| 114 | + return $e->getMessage(); |
| 115 | + } |
| 116 | + return $_json; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * _trestle_request - use cURL to send a request to Trestle |
| 121 | + * |
| 122 | + * @param string $url Url to send request to |
| 123 | + * @param string $method Method of request (POST/GET/PUT/DELETE) |
| 124 | + * @param array $_args POST/PUT key => value params |
| 125 | + * @return array returns result array |
| 126 | + */ |
| 127 | + private function _trestle_request($url,$method,&$_args = false) |
| 128 | + { |
| 129 | + $ch = curl_init(); |
| 130 | + curl_setopt($ch,CURLOPT_URL,$url); |
| 131 | + curl_setopt($ch,CURLOPT_MAXREDIRS,3); |
| 132 | + curl_setopt($ch,CURLOPT_FOLLOWLOCATION,0); |
| 133 | + curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); |
| 134 | + curl_setopt($ch,CURLOPT_VERBOSE,0); |
| 135 | + curl_setopt($ch,CURLOPT_HEADER,0); |
| 136 | + curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); |
| 137 | + curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); |
| 138 | + curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); |
| 139 | + curl_setopt($ch,CURLOPT_USERPWD,self::$__api_key .':'. self::$__api_secret); |
| 140 | + switch (strtoupper($method)) { |
| 141 | + case 'POST': |
| 142 | + curl_setopt($ch,CURLOPT_POST,true); |
| 143 | + curl_setopt($ch,CURLOPT_POSTFIELDS,$_args); |
| 144 | + break; |
| 145 | + case 'GET': |
| 146 | + curl_setopt($ch,CURLOPT_POST,false); |
| 147 | + curl_setopt($ch,CURLOPT_URL,$url .'?'. http_build_query($_args)); |
| 148 | + break; |
| 149 | + case 'PUT': |
| 150 | + curl_setopt($ch,CURLOPT_POSTFIELDS,$_args); |
| 151 | + curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT'); |
| 152 | + break; |
| 153 | + case 'DELETE': |
| 154 | + curl_setopt($ch,CURLOPT_POSTFIELDS,$_args); |
| 155 | + curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'DELETE'); |
| 156 | + break; |
| 157 | + } |
| 158 | + // Execute |
| 159 | + $json = curl_exec($ch); |
| 160 | + $code = curl_getinfo($ch,CURLINFO_HTTP_CODE); |
| 161 | + curl_close($ch); |
| 162 | + if ($code < 200 || $code > 299) { |
| 163 | + $_tmp = json_decode($json,true); |
| 164 | + throw new Exception($_tmp['error']); |
| 165 | + } |
| 166 | + return $json; |
| 167 | + } |
| 168 | +} |
0 commit comments