Get Vimeo Video URL

I want to share an interesting php script that allows us to get the url from a vimeo video.
As a special information, this was tested in PHP 5.4.11 version with cURL 7.25.0 version and I had problems with older php/curl versions.

<?php

function getVimeoVideoURL($id) {
    // get page with a player
    $queryResult = httpQuery('http://vimeo.com/' . $id);
    $content = $queryResult['content'];

    if (preg_match('#document\.getElementById\(\'player_(.+)\n#i', $content, $scriptBlock) == 0)
        return 1;

    preg_match('#"timestamp":([0-9]+)#i', $scriptBlock[1], $matches);
    $timestamp = $matches[1];
    preg_match('#"signature":"([a-z0-9]+)"#i', $scriptBlock[1], $matches);
    $signature = $matches[1];

    $url = 'http://player.vimeo.com/play_redirect?clip_id=' . $id . '&sig=' . $signature . '&time=' . $timestamp . '&quality=sd';

    // make the request for getting a video url
    $finalQuery = httpQuery($url);
    return $finalQuery['redirect_url'];
}

// make queries via CURL
function httpQuery($url) {

    $options = array(
        CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.168 Chrome/18.0.1025.168 Safari/535.19',
        CURLOPT_RETURNTRANSFER => true,
    );
    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $content = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    $result = $info;
    $result['content'] = $content;
    return $result;

}

echo getVimeoVideoURL(29274467);

?>
Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.