Examples - PHP - Telldus Live!

PHP example for Telldus Live!

This example shows the very basics things needed to be able to do API calls, no actual parsing of the response is done.

In order to run this example the server needs to have PEAR::HTTP_OAuth installed. You also need to obtain an API key and secret. These should be set in the file common.php

getRequestToken.php

To talk to Telldus server the application need to request a request token. We also need to supply a return url so the server knows where to redirect the user after he or she has authorized the application access to his or hers resources.

$consumer = new HTTP_OAuth_Consumer(constant('KEY'), constant('SECRET'));
$consumer->getRequestToken(constant(
    
'REQUEST_TOKEN'), constant('BASE_URL').'/getAccessToken.php'
);

When we have those we store them in the session.

$_SESSION['token'] = $consumer->getToken();
$_SESSION['tokenSecret'] = $consumer->getTokenSecret();

Redirect the user to the authorization page

$url $consumer->getAuthorizeUrl(
    
constant('AUTHORIZE_TOKEN')
);
header('Location:'.$url);

getAccessToken.php

The user always lands on this page after authorization, whether the autorization was successful or not.

PEAR::HTTP_OAuth throws an exception if the autorization was canceled so all we need to do is encapsulate evertything in a "try" block.

$consumer = new HTTP_OAuth_Consumer(
    
constant('KEY'), constant('SECRET'),
    
$_SESSION['token'], $_SESSION['tokenSecret']
);
try {
    
$consumer->getAccessToken(constant('ACCESS_TOKEN'));

Store the new token pair. In a real application we should store these values in the database at the current logged in user. These are the tokens we must use from now on when doing any call for that user.

$_SESSION['accessToken'] = $consumer->getToken();
$_SESSION['accessTokenSecret'] = $consumer->getTokenSecret();

At this point we don't need the request token anymore so we could overwrite them instead. This example still separates them.

Return to the first page

header('Location:index.php');

If the user didn't authorize this application, we must handle it.

} catch (Exception $e) {
    
?>
        <p>Authorization failed!</p>
        <p><a href="index.php">Go back</a></p>
    <?php
}

index.php

First a check if we have an access token, if not link to getRequestToken.php

if (!isset($_SESSION['accessToken'])) {
    
?>
        We have no access token, <a href="getRequestToken.php">connect us</a>
    <?php
    
exit();
}

The example then shows two API-calls. The first shows how to list all the devices belonging to a user. Please see the section "Device features" for the parameter supportedMethods.

$consumer = new HTTP_OAuth_Consumer(
    
constant('KEY'), constant('SECRET'),
    
$_SESSION['accessToken'], $_SESSION['accessTokenSecret']
);
$params = array(
    
'supportedMethods' =>
        
constant('TELLSTICK_TURNON') | constant('TELLSTICK_TURNOFF'),
);
$response $consumer->sendRequest(constant(
    
'REQUEST_URI').'/devices/list',
    
$params,
    
'GET'
);
echo 
'<pre>';
echo( 
htmlentities($response->getBody()));

The second example lists the clients belonging to the user.

$consumer = new HTTP_OAuth_Consumer(
    
constant('KEY'), constant('SECRET'),
    
$_SESSION['accessToken'], $_SESSION['accessTokenSecret']
);
$params = array();
$response $consumer->sendRequest(
    
constant('REQUEST_URI').'/client/list',
    
$params,
    
'GET'
);
echo 
'<pre>';
echo( 
htmlentities($response->getBody()));

Requirements

Code

Download