├── Five00PxPubClient.php └── README.md /Five00PxPubClient.php: -------------------------------------------------------------------------------- 1 | 10 | * // API options 11 | * $options = array( 12 | * 'key' => 'YOUR API KEY', 13 | * 'secret' => 'YOUR SECRET KEY', 14 | * ); 15 | * 16 | * // Initialize Cient 17 | * $five00Px = Five00PxPubClient::factory($options); 18 | * $tags = array( 19 | * // Tag 1 20 | * // Tag 2 21 | * ); 22 | * 23 | * // Search Params for the Photo 24 | * $apiParams = array ( 25 | * 'tag' => implode(',', $tags), 26 | * 'term' => 'Search Term', 27 | * 'rpp' => 40, 28 | * ); 29 | * 30 | * // Do the API call 31 | * try { 32 | * $photos = $five00Px->api('photos/search', $params); 33 | * } catch(Exception $e) { 34 | * echo 'Error in call ' . $e->getMessage(); 35 | * } 36 | * 37 | * var_dump($photos); 38 | * 39 | * 40 | * @see https://github.com/500px/api-documentation 41 | * @requires php_curl 42 | * @author Shih Oon Liong 43 | */ 44 | class Five00PxPubClient { 45 | 46 | const 47 | HTTP_METHOD_GET = 'GET', 48 | HTTP_METHOD_POST = 'POST', 49 | HTTP_RESPONSE_SUCCESS = 200, 50 | API_BASE_URL = 'https://api.500px.com'; 51 | 52 | var 53 | $logger = null, 54 | $version = 1, 55 | $defaultParams = array(); 56 | 57 | /** 58 | * Constructor 59 | * @param array $options Initialization options. The array must contain the follow indexes 60 | * 'key' => Your 500px Application Key 61 | * 'secret' => Your 500px Secret Key 62 | * 'version' => The version of the API. Default to 1 63 | */ 64 | public function __construct($options) { 65 | // Set Logger 66 | if ( array_key_exists('logger', $options) 67 | && ! empty ($options['logger']) ) { 68 | $this->logger = $options['logger']; 69 | } 70 | 71 | $this->consumerKey = $options['key']; 72 | $this->consumerSecret = $options['secret']; 73 | $this->defaultParams = array( 74 | 'consumer_key' => $this->consumerKey, 75 | 'consumer_secret' => $this->consumerSecret, 76 | ); 77 | 78 | // Set API version 79 | if ( array_key_exists('version', $options) ) { 80 | $this->version = $options['version']; 81 | } 82 | } 83 | 84 | public static function factory($options=array()) { 85 | $class = __CLASS__; 86 | $instance = new $class($options); 87 | 88 | return $instance; 89 | } 90 | 91 | /** 92 | * Log messages 93 | */ 94 | private function logMessage() { 95 | $logger = $this->logger; 96 | if ($logger == null) { 97 | return; 98 | } 99 | 100 | $arg_list = func_get_args(); 101 | call_user_func_array($logger, $arg_list); 102 | } 103 | 104 | /** 105 | * Do an API call on 500px 106 | * @param type $apiUrl 107 | * @return mixed 108 | * @throws Exception 109 | */ 110 | public function api($apiUrlCall, $userArgs=array(), $method='GET') { 111 | 112 | // Set up Call Args 113 | $this->logMessage('Default Args', $this->defaultParams); 114 | $args = array_merge($userArgs, $this->defaultParams); 115 | $this->logMessage('API Args', $args); 116 | 117 | $apiUrl = self::API_BASE_URL . '/v' . $this->version . '/' . $apiUrlCall; 118 | 119 | if ($method == self::HTTP_METHOD_GET) { 120 | $apiUrl .= '?' . http_build_query($args); 121 | } 122 | 123 | $data = null; 124 | try { 125 | $ch = curl_init(); 126 | 127 | // Set CURL options 128 | curl_setopt($ch, CURLOPT_URL, $apiUrl); 129 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 130 | 131 | $response = curl_exec($ch); 132 | $metadata = curl_getinfo($ch); 133 | 134 | if ( $response === false ) { 135 | throw new Exception('Invalid Response received'); 136 | } 137 | 138 | // Check Response 139 | if( array_key_exists('http_code', $metadata) 140 | && (int)$metadata['http_code'] == self::HTTP_RESPONSE_SUCCESS ) { 141 | $data = json_decode($response); 142 | curl_close($ch); 143 | } else { 144 | 145 | // Failed (non-success) Response received 146 | $errorMsg = 'Response error - Error encountered: ' 147 | . '[' . $metadata['http_code'] . '] ' 148 | . '(Request: ' . $apiUrl . ') '; 149 | if ( ! empty($response) ) { 150 | $data = json_decode($response); 151 | $errorMsg .= $data->error; 152 | } 153 | 154 | // Throw exception 155 | throw new Exception($errorMsg); 156 | 157 | } 158 | } catch (Exception $e) { 159 | // Failed to do curl call 160 | throw new Exception($e->getMessage()); 161 | } 162 | 163 | return $data; 164 | 165 | } 166 | } 167 | 168 | ?> 169 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 500px-api-client-public-php 2 | =========================== 3 | @requires php_curl 4 | @author Shih Oon Liong 5 | 6 | A simple PHP client to access the 500px API points that does is public (that does not require a user access token). 7 | To use the API with a 500px's user access token please refer to the official documentation 8 | 9 | @see https://github.com/500px/api-documentation 10 | 11 | Usage example to get photos: 12 | 13 | 14 | // API options 15 | $options = array( 16 | 'key' => 'YOUR API KEY', 17 | 'secret' => 'YOUR SECRET KEY', 18 | ); 19 | 20 | // Initialize Cient 21 | $five00Px = Five00PxPubClient::factory($options); 22 | $tags = array( 23 | // Tag 1 24 | // Tag 2 25 | ); 26 | 27 | // Search Params for the Photo 28 | $apiParams = array ( 29 | 'tag' => implode(',', $tags), 30 | 'term' => 'Search Term', 31 | 'rpp' => 40, 32 | ); 33 | 34 | // Do the API call 35 | try { 36 | $photos = $five00Px->api('photos/search', $params); 37 | } catch(Exception $e) { 38 | echo 'Error in call ' . $e->getMessage(); 39 | } 40 | 41 | var_dump($photos); 42 | 43 | --------------------------------------------------------------------------------