March 11th, 2010
C

PHP Viddler API Client – Forked

Okay that title sounds either a bit naughty or nasty, it’s neither. I forked the official PHP Viddler API wrapper and wrote a new one. Why? Well because when I worked there I wrote smaller client which worked out well for me so I’ve decided to release it.

If you strip out the comments from the new client, it’s less than 200 lines of code. Now I just focused on the core API, I didn’t add any of the add-on methods for embeds, oembed, etc. If you are looking for those I would suggest using the original Viddler client. I wanted to keep this lightweight, I may add it later on as an extension so if you want to use it you can, but for now, just keeping it simple.

So how did I strip so much out of the original? I used no 3rd party classes. I used only what is in PHP. I ditched the xmlparser.php file and went with SimpleXML. I also didn’t write a method for every for every possible API method. Actually I didn’t write one for any of them. I took advantage of PHP’s magic method __call(). Which is automatically called when the user calls an object method that doesn’t exist.

$viddler = new Viddler('API KEY');
$res = $viddler->user_auth(array(
  'user'      =>  'USERNAME'
  'password'  =>  'YOUR PASS'
));

Requirements

  1. PHP
  2. cURL
  3. SimpleXML
  4. json_encode
  5. Viddler API Key

Calling Methods

See the method user_auth() doesn’t exist, but it doesn’t matter. The class figures out that is should call the API method viddler.users.auth. I wrote a method in the class that gives the end user a lot of flexibility. It breaks down the method by the official namespaces and stitches it back together correctly. You have a variety of ways you can actually call the method, below are some examples.

$viddler->viddler_users_auth($params);
$viddler->users_auth($params);
$viddler->viddlerUsersAuth($params);
$viddler->usersAuth($params);
$viddler->usersauth($params);
$viddler->viddlerusersauth($params);
$viddler->viddlerusersAuth($params);

You can start the method with ‘viddler’ or just ditch it. I chose to ditch it, because it’s shorter. I left it in there in case in there are other first level namespaces. After that choice you then write the second section of the namespace. Currently they are: users, videos and api. Then the last part is the actual API method name. You can use undescores or camelCase it up to you. The only requirement is that if the last section, the API method is camelCase, you must also camelCase. So if you are calling viddler.videos.getByUser, getByUser must be camelCase. So you could write the previous as any of the following:

$viddler->viddler_videos_getByUser($params);
$viddler->videos_getByUser($params);
$viddler->viddlerVideosGetByUser($params);
$viddler->viddlerVideosgetByUser($params);
$viddler->videosGetByUser($params);
$viddler->videosgetByUser($params);

All are acceptable :) .

The $params argument is an array of all the actual arguments from the documentation for that method. The key would be the variable name, spelled as it is in the documentation and the value would be the value you want to send.

POST, HTTPS and BINARY FILES

The class figures all this out for you. If the method you are calling is to be sent via POST, no worries. If the method has the option to be sent over HTTPS, it’s done for you. And if you are uploading a video or a thumbnail, not an issue, it will figure out exactly what to do.

Response Type

Currently the Viddler API only returns XML. The original Viddler wrapper converts to an array via the xmlparser.php file. I added the ability to get the response back as the actual XML, PHP array or even JSON. How, pretty simple. Parse the XML via SimpleXML and turn into an array if need be, or even json. I don’t know how you want your data back, so why not send it how you would like to get it. These three seem like the most popular API response types, so that’s what I went with.

You can set the response type by setting the class parameter $response_type and even set it per method call. You have the ability to change it in each call if you’d like. The default is ‘php’ which will return an array of course.

//Parameter
$viddler->response_type = 'json';

//Thru method call
$res = $viddler->user_auth(array(
  'user'           =>  'USERNAME'
  'password'       =>  'YOUR PASS',
  'response_type'  =>  'json'
));

API Key

Just as you can set the response type two ways, you can set the api key multiple ways. You can set the api key by setting the class parameter $api_key, set it when you create the object and send it when you call any method. You have the ability to change it in each call if you’d like.

//Parameter
$viddler->api_key = 'YOUR KEY';

//As you create the object
$viddler = new Viddler('YOUR KEY');

//Thru method call
$res = $viddler->user_auth(array(
  'user'        =>  'USERNAME'
  'password'    =>  'YOUR PASS',
  'api_key'     =>  'YOUR KEY'
));

Download

You can download this class over at GitHub here. Or you can even fork it if you’d like. It would be a forked fork, not sure if that is even legal. Check with your state officials first. Lame attempt at humor.

You can leave a reply, or trackback from your own site.

One Response to this article.

  1. Colin Devroe says:

    Simply beautiful. Thanks for sharing this.

    I vote we somehow figure out a way to merge this one with a few of the must-have features of the old one. Example must-have: adding and removing tags from a video. Right now PHPViddler has a method for that while the API itself does not. (there are a few things like that).

    I also like that this fork takes https:// into consideration too (I just made that update to PHPViddler myself).

    Again, fantastic and thanks for sharing.

Leave a Reply