/Petfinder.php

https://github.com/wilhelser/Petfinder · PHP · 326 lines · 123 code · 48 blank · 155 comment · 10 complexity · 712e853baeae79884471c53b98596255 MD5 · raw file

  1. <?php
  2. /**
  3. *----------------------------------------
  4. * PHP Client Library for Petfinder.com API
  5. * @author Brian Haveri
  6. * @link http://github.com/brianhaveri/petfinder
  7. * @license MIT License http://en.wikipedia.org/wiki/MIT_License
  8. *----------------------------------------
  9. */
  10. class Petfinder {
  11. // Only modify if official API changes.
  12. private $_apiUrl = 'http://api.petfinder.com/';
  13. private $_validResponseFormats = array('json', 'xml');
  14. private $_methodsRequiringToken = array();
  15. // Default values. You can modify these.
  16. private $_responseFormat = 'xml';
  17. // Intermediate storage. Do not modify.
  18. private $_apiKey;
  19. private $_apiSecret;
  20. private $_lastRequest;
  21. private $_token;
  22. /**
  23. *----------------------------------------
  24. * Create Petfinder instance
  25. * @param string $apiKey API Key
  26. * @param string $apiSecret API Secret
  27. *----------------------------------------
  28. */
  29. function __construct($apiKey, $apiSecret=NULL) {
  30. $this->_apiKey = $apiKey;
  31. $this->_apiSecret = $apiSecret;
  32. }
  33. /**
  34. *----------------------------------------
  35. * Set API response format
  36. * @param string $format
  37. * @return bool TRUE on success, FALSE on fail
  38. *----------------------------------------
  39. */
  40. public function setResponseFormat($format) {
  41. if(in_array($format, $this->_validResponseFormats)) {
  42. $this->_responseFormat = $format;
  43. return TRUE;
  44. }
  45. return FALSE;
  46. }
  47. /**
  48. *----------------------------------------
  49. * Get the last request
  50. * @return string Request
  51. *----------------------------------------
  52. */
  53. public function getLastRequest() {
  54. return $this->_lastRequest;
  55. }
  56. /**
  57. *----------------------------------------
  58. * Get the token
  59. * @return string
  60. *----------------------------------------
  61. */
  62. public function getToken() {
  63. return $this->_token;
  64. }
  65. /**
  66. *----------------------------------------
  67. * Set the token
  68. * @param string $token
  69. * @return bool
  70. *----------------------------------------
  71. */
  72. public function setToken($token) {
  73. $this->_token = $token;
  74. return TRUE;
  75. }
  76. /**
  77. *----------------------------------------
  78. * Petfinder API: Returns a token valid for a timed session
  79. * @link http://www.petfinder.com/developers/api-docs
  80. * @return string
  81. *----------------------------------------
  82. */
  83. public function auth_getToken() {
  84. return $this->_callMethod(__FUNCTION__, array('sig'=>$this->_getSignature()));
  85. }
  86. /**
  87. *----------------------------------------
  88. * Petfinder API: Returns a list of breeds for a particular animal.
  89. * @link http://www.petfinder.com/developers/api-docs
  90. * @param mixed $data Array with 'animal' key OR string of animal name
  91. * @return string
  92. *----------------------------------------
  93. */
  94. public function breed_list($data) {
  95. if(! is_array($data)) {
  96. $data = array('animal'=>(string) $data);
  97. }
  98. return $this->_callMethod(__FUNCTION__, $data);
  99. }
  100. /**
  101. *----------------------------------------
  102. * Petfinder API: Returns a record for a single pet.
  103. * @link http://www.petfinder.com/developers/api-docs
  104. * @param mixed $data Array with 'id' key OR integer of pet id
  105. * @return string
  106. *----------------------------------------
  107. */
  108. public function pet_get($data) {
  109. if(! is_array($data)) {
  110. $data = array('id'=>(string) $data);
  111. }
  112. return $this->_callMethod(__FUNCTION__, $data);
  113. }
  114. /**
  115. *----------------------------------------
  116. * Petfinder API: Returns a record for a randomly selected pet.
  117. * @link http://www.petfinder.com/developers/api-docs
  118. * @param array $data
  119. * @return string
  120. *----------------------------------------
  121. */
  122. public function pet_getRandom($data=array()) {
  123. return $this->_callMethod(__FUNCTION__, $data);
  124. }
  125. /**
  126. *----------------------------------------
  127. * Petfinder API: returns a collection of pet records.
  128. * @link http://www.petfinder.com/developers/api-docs
  129. * @param mixed $data Array with 'location' key OR string of location
  130. * @return string
  131. *----------------------------------------
  132. */
  133. public function pet_find($data) {
  134. if(! is_array($data)) {
  135. $data = array('location'=>(string) $data);
  136. }
  137. return $this->_callMethod(__FUNCTION__, $data);
  138. }
  139. /**
  140. *----------------------------------------
  141. * Petfinder API: Returns a collection of shelter records matching your search criteria.
  142. * @link http://www.petfinder.com/developers/api-docs
  143. * @param mixed $data Array with 'location' key OR string of location
  144. * @return string
  145. *----------------------------------------
  146. */
  147. public function shelter_find($data) {
  148. if(! is_array($data)) {
  149. $data = array('location'=>(string) $data);
  150. }
  151. return $this->_callMethod(__FUNCTION__, $data);
  152. }
  153. /**
  154. *----------------------------------------
  155. * Petfinder API: Returns a record for a single shelter.
  156. * @link http://www.petfinder.com/developers/api-docs
  157. * @param mixed $data Array with 'id' key OR string of shelter id
  158. * @return string
  159. *----------------------------------------
  160. */
  161. public function shelter_get($data) {
  162. if(! is_array($data)) {
  163. $data = array('id'=>(string) $data);
  164. }
  165. return $this->_callMethod(__FUNCTION__, $data);
  166. }
  167. /**
  168. *----------------------------------------
  169. * Petfinder API: Returns a list of IDs or collection of pet records for an individual shelter
  170. * @link http://www.petfinder.com/developers/api-docs
  171. * @param mixed $data Array with 'id' key OR string of shelter id
  172. * @return string
  173. *----------------------------------------
  174. */
  175. public function shelter_getPets($data) {
  176. if(! is_array($data)) {
  177. $data = array('id'=>(string) $data);
  178. }
  179. return $this->_callMethod(__FUNCTION__, $data);
  180. }
  181. /**
  182. *----------------------------------------
  183. * Petfinder API: Returns a list of shelter IDs listing animals of a particular breed.
  184. * @link http://www.petfinder.com/developers/api-docs
  185. * @param array $data
  186. * @return string
  187. *----------------------------------------
  188. */
  189. public function shelter_listByBreed($data) {
  190. return $this->_callMethod(__FUNCTION__, $data);
  191. }
  192. /**
  193. *----------------------------------------
  194. * Call an API method
  195. * @param string $method Method name
  196. * @param array $data
  197. * @return string
  198. *----------------------------------------
  199. */
  200. private function _callMethod($method, $data=array()) {
  201. // Get a token if necessary
  202. if(in_array($method, $this->_methodsRequiringToken)) {
  203. if(! $this->getToken()) {
  204. return FALSE;
  205. }
  206. $data['token'] = $this->getToken();
  207. }
  208. $request = $this->_getRequest($method, $data);
  209. if($method === 'auth_getToken') {
  210. $request = str_replace('&amp;', '&', $request);
  211. }
  212. $this->_lastRequest = $request;
  213. return $this->_getResponse($request);
  214. }
  215. /**
  216. *----------------------------------------
  217. * Send an API request and receive a response
  218. * @param string $request
  219. * @return array $data
  220. *----------------------------------------
  221. */
  222. private function _getResponse($request) {
  223. $curlOptions = array(
  224. CURLOPT_URL => $request,
  225. CURLOPT_RETURNTRANSFER => TRUE,
  226. CURLOPT_USERAGENT => __CLASS__.' PHP Client Library'
  227. );
  228. $ch = curl_init();
  229. curl_setopt_array($ch, $curlOptions);
  230. $data = curl_exec($ch);
  231. curl_close($ch);
  232. return $data;
  233. }
  234. /**
  235. *----------------------------------------
  236. * Generate an API request string
  237. * @param string $method
  238. * @param array $data
  239. * @return string
  240. *----------------------------------------
  241. */
  242. private function _getRequest($method, $data=array()) {
  243. // Argument order matters for auth_getToken().
  244. // For consistency, always use boilerPlateData, then data
  245. $boilerPlateData = array(
  246. 'key' => $this->_apiKey,
  247. 'format'=> $this->_responseFormat
  248. );
  249. $data = array_merge($boilerPlateData, (array) $data);
  250. return join('', array(
  251. $this->_apiUrl,
  252. $this->_convertMethod($method),
  253. '?',
  254. http_build_query($data)
  255. ));
  256. }
  257. /**
  258. *----------------------------------------
  259. * Generate an API signature
  260. * @return string Signature
  261. *----------------------------------------
  262. */
  263. private function _getSignature() {
  264. return md5($this->_apiSecret.'key='.$this->_apiKey.'&format='.$this->_responseFormat);
  265. }
  266. /**
  267. *----------------------------------------
  268. * Convert a method name into API method name format
  269. * Example: pet_find => pet.find
  270. * @param string $method
  271. * @return string
  272. *----------------------------------------
  273. */
  274. private function _convertMethod($method) {
  275. return str_replace('_', '.', $method);
  276. }
  277. }
  278. ?>