PHP SDK


The PHP SDK provides access to the features of the Platform API.

It is available on GitHub. Feel free to report any issue or send pull requests there.


Overview

The PHP SDK implements the Dailymotion Platform API. For a list of all available methods, see the complete API reference.

To call a method using the PHP SDK, use the GET, POST or DELETE methods as follows:

$api = new Dailymotion();
$result = $api->get(
    '/videos',
    array('fields' => array('id', 'title', 'owner'))
);

The $result variable contains the result of the method (as described in the response types) as an array.


Authentication

The Platform API requires OAuth 2.0 authentication in order to be used.

Contrary to most OAuth SDKs, the Dailymotion PHP SDK implements lazy authentication, meaning no authentication request is sent until data is actually requested. When that happens, two requests are sent back-to-back during the first attempted API call: one to authenticate, one to fetch the data.

The Dailymotion PHP SDK also abstracts the entire OAuth flow, from retrieving, storing and using access tokens, to automatically refreshing them. If needed, access tokens can still be handled manually using Dailymotion::getSession() and Dailymotion::setSession().

Example of a session returned by the OAuth server:

{
    "access_token": "<ACCESS_TOKEN>",
    "token_type": "Bearer",
    "expires_in": 36000,
    "refresh_token": "<REFRESH_TOKEN>",
    "scope": "<SCOPE1 SCOPE2 SCOPE3>",
    "uid": "<USER_ID>"
}

Note: OAuth 2.0 does not support upgrading scopes on an existing session. To request new scopes, you must call Dailymotion::logout() and restart a new session.

If you wish to retrieve an access token manually (not recommended), use Dailymotion::getAccessToken().

The library supports three OAuth 2.0 grant types.

Authorization grant type

This grant type should be used in most cases. Users are redirected to the Dailymotion authorization page and your application receives a callback once access is granted.

// Instantiate the PHP SDK.
$api = new Dailymotion();

// Choose the authentication grant type.
$api->setGrantType(Dailymotion::GRANT_TYPE_AUTHORIZATION, $apiKey, $apiSecret);

try {
    $result = $api->get(
        '/me/videos',
        array('fields' => array('id', 'title', 'owner')
    );
}
catch (DailymotionAuthRequiredException $e) {
    return header('Location: ' . $api->getAuthorizationUrl());
}
catch (DailymotionAuthRefusedException $e) {
    // Handle user refusal
}

Password grant type

Use this when your PHP application cannot redirect the user (e.g. CLI scripts). You must collect user credentials yourself. Never expose the API secret.

$api = new Dailymotion();

if (empty($_POST['username']) || empty($_POST['password'])) {
    // Ask for credentials
} else {
    $api->setGrantType(
        Dailymotion::GRANT_TYPE_PASSWORD,
        $apiKey,
        $apiSecret,
        array(),
        array(
            'username' => $_POST['username'],
            'password' => $_POST['password'],
        )
    );

    $result = $api->get(
        '/me/videos',
        array('fields' => array('id', 'title', 'owner'))
    );
}

Client Credentials grant type

Use this when you do not need access on behalf of a user. This gives access only to public data or data permitted by your scopes/roles.

$api = new Dailymotion();
$api->setGrantType(Dailymotion::GRANT_TYPE_CLIENT_CREDENTIALS, $apiKey, $apiSecret);

$result = $api->get(
    '/videos',
    array('fields' => array('id', 'title', 'owner'))
);

No authenticated user exists, so /me endpoints cannot be accessed.


Upload File

Some methods like POST /me/videos require a file URL. The SDK provides a temporary upload helper using Dailymotion::uploadFile().

$url = $api->uploadFile($filePath);
var_dump($url);

Then use this URL when creating a video:

$result = $api->post(
    '/me/videos',
    array(
        'url' => $url,
        'title' => $videoTitle,
        'channel' => $channel,
        'published' => true,
        'is_created_for_kids' => false
    )
);

Retrieve a progress URL:

$progressUrl = null;
$url = $api->uploadFile($filePath, null, $progressUrl);
var_dump($progressUrl);

Querying this URL lets you monitor the upload progress.


Overloading the SDK

You can override the SDK to customize OAuth handling. Commonly, developers override Dailymotion::storeSession() and Dailymotion::readSession() to change the default cookie storage.

Example using a file-based session for CLI environments:

class DailymotionCli extends Dailymotion
{
    protected static $sessionFile;

    public function __construct()
    {
        self::$sessionFile = __DIR__ . '/api-session.json';
    }

    protected function readSession()
    {
        $session = json_decode(file_get_contents(self::$sessionFile), true);
        return $session;
    }

    protected function storeSession(array $session = array())
    {
        file_put_contents(self::$sessionFile, json_encode($session), LOCK_EX);
        return $this;
    }
}

Feel free to extend the SDK and submit pull requests. If you spot any issue, open a ticket.