Get direct link video from Vimeo in PHP

I need a direct link to a video from Vimeo with a PHP script. I managed to find them manually, but my PHP script is not working. Here is the initiative: For example, I took this video: http://vimeo.com/22439234

When you go to the page, Vimeo generates a signature associated with the current timestamp and this video. This information is stored in a JavaScript variable on line 520 immediately after: window.addEvent ('domready', function () {

Then, when you click Play, the HTML5 player reads this variable and sends an HTTP request:

 http:// player.vimeo.com/play_redirect?clip_id=37111719&sig={SIGNATURE}&time={TIMESTAMP}&quality=sd&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location= 

But it also works with:

 http:// player.vimeo.com/play_redirect?clip_id=37111719&sig={SIGNATURE}&time={TIMESTAMP}&quality=sd 

If this URL does not open with the IP address that http://vimeo.com/22439234 has opened, it returns an HTTP 200 code with an error message.

If this URL is opened with the correct IP address, the Location header is redirected to the video link: http://av.vimeo.com/XXX/XX/XXXX.mp4?aksessionid=XXXX&token=XXXXX_XXXXXXXXX

When I create this link http://player.vimeo.com/play_redirect?... manually ("right click"> "source code"> "line 520"), it works.

But with PHP and regex, it returns an HTTP code 200 with an error message.

Why?

From my observations, Vimeo does not check the HTTP request headers for http:// player.vimeo.com/play_redirect?... GET , HEAD , with cookies, without cookies, referrer, etc ... does not change.

With PHP, I use the functions file_get_contents() and get_headers() .

  <?php function getVimeo($id) { $content = file_get_contents('http://vimeo.com/'.$id); 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'; print_r(get_headers($url, 1)); } 
+8
php download vimeo
source share
2 answers

The algorithm is as follows:

  • Input: vimeoUrl.
  • content = getRemoteContent (vimeoUrl).
  • Parsing content to find and retrieve values ​​from the data-config-url attribute.
  • Go to data-config-url and load the content as a JSON object: $ video = json_decode ($ this-> getRemoteContent ($ video-> getAttribute ('data-config-url')));
  • Return $ video-> request-> files-> h264-> sd-> url - this will return a direct link for SD quality video.

Here is my simple class that works at this point.

 class VideoController { /** * @var array Vimeo video quality priority */ public $vimeoQualityPrioritet = array('sd', 'hd', 'mobile'); /** * @var string Vimeo video codec priority */ public $vimeoVideoCodec = 'h264'; /** * Get direct URL to Vimeo video file * * @param string $url to video on Vimeo * @return string file URL */ public function getVimeoDirectUrl($url) { $result = ''; $videoInfo = $this->getVimeoVideoInfo($url); if ($videoInfo && $videoObject = $this->getVimeoQualityVideo($videoInfo->request->files)) { $result = $videoObject->url; } return $result; } /** * Get Vimeo video info * * @param string $url to video on Vimeo * @return \stdClass|null result */ public function getVimeoVideoInfo($url) { $videoInfo = null; $page = $this->getRemoteContent($url); $dom = new \DOMDocument("1.0", "utf-8"); libxml_use_internal_errors(true); $dom->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $page); $xPath = new \DOMXpath($dom); $video = $xPath->query('//div[@data-config-url]'); if ($video) { $videoObj = json_decode($this->getRemoteContent($video->item(0)->getAttribute('data-config-url'))); if (!property_exists($videoObj, 'message')) { $videoInfo = $videoObj; } } return $videoInfo; } /** * Get vimeo video object * * @param stdClass $files object of Vimeo files * @return stdClass Video file object */ public function getVimeoQualityVideo($files) { $video = null; if (!property_exists($files, $this->vimeoVideoCodec) && count($files->codecs)) { $this->vimeoVideoCodec = array_shift($files->codecs); } $codecFiles = $files->{$this->vimeoVideoCodec}; foreach ($this->vimeoQualityPrioritet as $quality) { if (property_exists($codecFiles, $quality)) { $video = $codecFiles->{$quality}; break; } } if (!$video) { foreach (get_object_vars($codecFiles) as $file) { $video = $file; break; } } return $video; } /** * Get remote content by URL * * @param string $url remote page URL * @return string result content */ public function getRemoteContent($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_USERAGENT, 'spider'); $content = curl_exec($ch); curl_close($ch); return $content; } } 

Using:

 $video = new VideoController; var_dump($video->getVimeoDirectUrl('http://vimeo.com/90747156')); 
+4
source share

Try adding a valid user agent to the headers for each request. To do this, you should use cURL or HttpRequest instead of file_get_contents ().

After such manipulations, I got a working link to download the video file.

Here is my code:

 function getVimeo($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 #print_r(get_headers($url, 1)); $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 getVimeo(22439234); 
0
source share

All Articles