Download the class :Upcoming.php API v0.1
About
After looking for a decent Upcoming.org API Wrapper for PHP5, it seems that were isn't one that's available. And, since I need one for a project I'm doing I thought I may as well put some effort into making a reasonably polished class that other people could use! So, here it is.
Requirements
Not a lot!
- PHP5
- HTTP_Request - (pear install HTTP_Request)
- SimpleXML
There's no need for a webserver or anything like that. As long as your machine can talk to upcoming.yahooapis.com then you're good to go.
Perhaps one day I'll get it to PEAR standards.
Simple event.getInfo call
Code required is minimal, a better commented version is available in the source. The method to call on the object is the same a s in the Upcoming API documentation, except with underscores instead of dots. i.e. event.getInfo would be event_getInfo().
The parameters are sent to the API call using an associative array.
$client = new UpcomingWrapper($api_key);
$response = $client->event_getInfo(array("event_id"=>42));
foreach ($response->getResults() as $item) {
echo $item->name;
}
Responses are stored as recordsets, which can be accessed as an array using the getResults() method. Each record can have its attributes accessed using magic-public fields (i.e. $item->name). To return the type of the object, use ->getType();
Here is a working example:
| What kind of object are you? | event |
| Name | Peaches, Electrocute, xBxRx |
| Description | Trash rock at its finest |
Authentication
Upcoming API uses token based authentication. To learn about this, go to the Upcoming API Token Based Authentication page.
For token based authentication, you'll need to sort out your own callback using the Upcoming.org API Key gen, and once you have obtained your 'frob' you can then use the ->frobToToken() method to enable it's use in requests.
Given that the frob was correctly handled, the token will be sent with every request from there after (TODO: make this process intelligent so it only send the token when required)
The entire token is stored in the object, including userid. This can be accessed by calling $client->getToken();
Once you have the token object, you can serialize it and store it wherever you wish, and replace it later with the $client->setToken($token_obj); method
Caching
To enable caching, call $client->setCacheDir($path) on the object after initialising. If you wish to change the TTL of the cached files from the default of 1 hour, use $client->setCacheTTL($ttl) to set the number of minutes for the cache to be valid for.
Cache files will be automatically cleaned up periodically.
Download the class :Upcoming.php