If you haven’t already, please be sure to read part one that Kyle put together, which covers the simple task of initiating the phpViddler class and using it to list videos. Some of the code found herein may not make sense if you haven’t read part one.
I think you’ll find, through these tutorials, that using phpViddler makes it dead simple to use Viddler’s APIs. Please leave feedback if you have any specific areas you’d like us to cover.
Authenticating a user
To authenticate a user you will need both the username and password of the user. For this example, we’ll say the username is my username, cdevroe, and the password will be a fake password of ‘password’.
There are a few important points to remember when authenticating a user using Viddler’s API, whether you use phpViddler or not. Each time you authenticate a user you are giving a sessionID representing a “session” for that username and password. If you are looking to log in a different user, you will need to re-authenticate the credentials to get another sessionID.
As we move forward with this series, you will learn more about where you use a sessionID to do things like changing meta data, uploading video, etcetera.
To start a new session, this is the code you will need to use once you have the username and password for the user.
<?php // Authenticate the user, return array of response $sessionResponse = $viddler->user_authenticate('cdevroe','password'); ?>
Check for errors before you do anything. Typical authentication errors are that the username does not exist (error code 101), or the password provided is incorrect (error code 103).
Once you’ve determined there are no errors, you can now put the sessionID into a variable for use later. In our next parts, we’ll be using the sessionID in a number of ways.
<?php // Store sessionID in variable. $sessionID = $sessionResponse['auth']['sessionid']; ?>
Notes about sessions
SessionIDs expire after 15 minutes of inactivity. You should build your application with this in mind. For example, if you build an uploading tool, and the upload of a video file takes longer than 15 minutes, the session you are using to upload a video with will expire before your upload is finished. Don’t panic! All you need to do is ask for a new one and you’re all set.
(Super side note: If you come to a point in your code where you need a sessionID, you can quickly run any method that requires a sessionID to see if the session is still valid, if it isn’t, get a new one, and continue your code.)
In our next part, we’ll be covering the use of our Record with Webcam API and uploading video files.
If you have any questions about authenticating users, or sessions in general, feel free to ask.