Instantly share code, notes, and snippets.

Embed
What would you like to do?
An easy to use PHP Class to get Download Links from Vimeo.com - Current Version: 1.0
<?php
/**
* This source code was created by Stefan Kühnel and the copyright is the sole property of the following natural and legal persons:
* Copyright (c) 2018 Stefan Kühnel -> https://stefankuehnel.com
* Copyright (c) 2018 Bealeaf Network -> https://www.bealeaf.net
* Copyright (c) 2018 Alidrin.com -> https://www.alidrin.com
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Bealeaf.
*
* As with any software that integrates with the Bealeaf platform, your use
* of this software is subject to the Bealeaf Developer Principles and
* Policies [https://9bn.de/bosl?lang=en]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
class VimeoDownloader
{
public function __construct($VimeoVideoURL)
{
// Store Vimeo Video URL in $this->VimeoVideoURL
$this->VimeoVideoURL = htmlspecialchars($VimeoVideoURL);
}
public function getVimeoDownloadLinks() {
// Store Download Links from https://player.vimeo.com/video/{videoId}/config?... in Array $downloadLinks;
$downloadLinks = array('original' => $this->parseVimeoPageContent()["request"]["files"]["progressive"][0]["url"]);
// Return $downloadLinks Array
return $downloadLinks;
}
private function cURL() {
// Define multiple UserAgents in order to distract Vimeo's Security Center
$userAgent = array(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.5 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'
);
// Get HTML from https://vimeo.com/{videoId}
$cURL = curl_init($this->VimeoVideoURL);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_BINARYTRANSFER, true);
curl_setopt($cURL, CURLOPT_USERAGENT, $userAgent[rand(0, 4)]);
$cURLResponse = curl_exec($cURL);
curl_close($cURL);
// Return HTML Content
return $cURLResponse;
}
// Get all URLs from JavaScript Source Code
private function returnSourceCodeURLs($string) { // Code provided by Kendall Hopkins -> https://stackoverflow.com/a/2790919
$regex = '/https?\:\/\/[^\" ]+/i';
preg_match_all($regex, $string, $matches);
// Return all found URLs as Array
return ($matches[0]);
}
private function parseVimeoPageContent()
{
// Create new DomDocument Object (Required because DOMXPath() needs DomDocument() to run properly)
$DomDocument = new DOMDocument();
// Set error level
$internalErrors = libxml_use_internal_errors(true);
// Load the HTML Content from cURL() Response into DomDocument object
$DomDocument->loadHTML($this->cURL());
// Restore error level
libxml_use_internal_errors($internalErrors);
// Create new DOMXPath object
$xPathObject = new DOMXPath($DomDocument);
// Get all JavaScript Elements without src="" attributes of cURL HTML Content
$returnJavaScript = $xPathObject->query('//body//script[not(@src)]');
// Convert DomDocument() object to Array with iterator_to_array() Function.
$returnJavaScriptAsArray = iterator_to_array($returnJavaScript);
// Convert https:\/\/player.vimeo.com\/video\/{videoId}\/config?... from HTML Content to https://player.vimeo.com/video/{videoId}/config?...
// Notice: $returnJavaScriptAsArray[3] = JavaScript with https://player.vimeo.com/video/{videoId}/config?... URLs
// Notice: $returnJavaScriptAsArray[3]->nodeValue holds JavaScript Content as Text
$search = array('\/\/', '\/');
$replace = array('//', '/');
$returnValidURLs = str_replace($search, $replace, $returnJavaScriptAsArray[3]->nodeValue);
// Get all URLs that starts with https://player.vimeo.com. The URL https://player.vimeo.com/video/{videoId}/config?... contains the download links as valid JSON
foreach ($this->returnSourceCodeURLs($returnValidURLs) as $validURLs) {
if (substr( $validURLs, 0, 24 ) === "https://player.vimeo.com") {
// Store all found urls in Array $returnJSONContentURLs
$returnJSONContentURLs[] = $validURLs;
}
}
// Get Content of the Domain https://player.vimeo.com/video/{videoId}/config?...
$jsonData = file_get_contents($returnJSONContentURLs[0]);
// Decode JSON Data and create associative Array with "true"-flag
$decodeJsonData = json_decode($jsonData, true);
// Return the associative Array
return $decodeJsonData;
}
}
?>
@stefankuehnel

This comment has been minimized.

Owner

stefankuehnel commented Sep 4, 2018

Usage

You can use the code as follows:

<?php
require 'VimeoDownloader.class.php';

$Vimeo = new VimeoDownloader($_GET["url"]);

echo $Vimeo->getVimeoDownloadLinks()['original'];
?>

License:

Bealeaf Open Source License -> https://9bn.de/bosl?lang=en

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment