If the documentation exists that shows how easy it is to use to 500px api, I’ve not been able to find it. This post resolves that by sharing my findings and the solution I’m using in a WordPress plugin I’ve been working on.
I wanted to tweet photos from 500px at random intervals to improve the visual impact of my twitter feed.
Using the 500px api…
It sounds simple enough but I hit a roadblock: I couldn’t find an example for how to authenticate using oauth and call their api. The best I found was this example provided as part of their official documentation. It doesn’t give a solution — it suggests edits to an existing piece of code to get you started. One line on that page was a massive help:
500px.com oAuth is actually quite similar to Twitter’s
Hmmm… I already had a working connection to Twitter based on the incredibly easy to use TwitterAPIExchange.php code by J7mbo. That got me thinking:
What if I could reuse that code?
It was a far better solution than the original page’s suggestion so I decided to give it a go.
Step 1: Register a new 500px app…
…to get your consumer keys, required by oauth to authenticate and gain access to the apis. (You can do this by logging in to 500px and visiting the Account | Settings page.)
This is the first difference: unlike Twitter which requires a pair of keys and a pair of tokens, the 500px api only provides you with a pair of keys: Consumer key and Secret key.
What about the tokens? As it turns out, you don’t actually need them for 500px.
Step 2: Setting up your keys for oauth…
This is super simple as shown in the code fragment below.
[snippet slug=500px-oauth-setup lang=php]
Step 3: Specifying the api and request…
For this example we’re going to grab a set of 20 images from the ‘Highest Rated’ collection using the ‘photos’ api (documentation available here). The next code fragment shows how I set these up, note there is a subtle difference here from the original Twitter example given by J7mbo.
[snippet slug=500px-api-parameters lang=php]
Step 4: Call the api…
This just boils down to create the object, call the functions with the parameters we set up in steps 1 and 2.
[snippet slug=500px-authenticate-call-api lang=php]
Note the TwitterAPIExchange method calls can be daisy chained together. I chose not to here as a matter of personal coding style choice because I find the code much easier to read.
Step 5: Decode the output…
The result of the api call is a JSON encoded array containing the data pertaining to 20 images on 500px. To make it useful, we need to decode it, rewriting the last line in the snippet above thus…
[snippet slug=500px-json-decode-api-result lang=php]
Step 6: Adding a little protection
During testing I hit a snag. I’d built it into a WordPress plugin and the code worked fine on my MacBook running MAMPP, but crashed under XAMPP on Windows. I traced it to the TwitterAPIExchange class throwing an exception in the call to performRequest().
Trapping it with a try-catch block revealed the issue to be a missing ssl certificate on my XAMPP installation. I’ve shown that modification to the code in the next code fragment.
[snippet slug=500px-exception-handling lang=php]
The (almost) completed code…
[snippet slug=500px-complete-api-call lang=php]
The last snippet, above, shows our final function. It’s a cut down version of the one I’m using within the plugin for clarity. It will need to be modified to suit your own needs. For example making it more generic by passing the api URL, feature and method.
If you do use it, please drop me a comment below 🙂