从PHP中获取Vimeo的直接链接视频 [英] Get direct link videos from Vimeo in PHP

查看:356
本文介绍了从PHP中获取Vimeo的直接链接视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接链接到Vimeo的视频,并带有一个PHP脚本。
我设法手动找到它们,但我的PHP脚本不起作用。
这是倡议:
例如我拍了这个视频: http://vimeo.com/22439234



当您进入页面时,Vimeo会生成与当前时间戳和此视频关联的签名。这个信息存储在一个JavaScript变量中,就在之前的一行520下:
window.addEvent('domready',function(){



然后,当您单击播放时,HTML5播放器读取此变量并发送HTTP请求:

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

但它也适用于:

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

如果此网址未打开,则打开 http://vimeo.com/22439234 ,这将返回HTTP代码200,并显示一条错误消息。



如果此URL打开,正确的IP地址,标题位置重定向到链接视频文件:
http://av.vimeo.com/XXX/XX/XXXX.mp4?aksessionid=XXXX&token=XXXXX_XXXXXXXXX



当我构建此链接 http://player.vimeo.com/play_redirect?... 手动(右键单击 >source code>line 520)它的工作原理。



但是使用PHP和regex,它返回 HTTP代码200 有一个错误消息。



为什么?



从我的观察,Vimeo不检查标题HTTP请求 http:// player.vimeo.com/play_redirect?...
GET HEAD ,带有Cookie,无Cookie,引荐来源等...不会更改。



使用PHP ,我使用函数 file_get_contents() get_headers()

 <?php 
函数getVimeo($ id){

$ content = file_get_contents('http://vimeo.com/ 的$ id);

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

preg_match('#timestamp:([0-9] +)#i',$ scriptBlock [1],$ matches);
$ timestamp = $ match [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));
}


解决方案

算法如下所示:




  • 输入数据:vimeoUrl。

  • content = getRemoteContent(vimeoUrl)。

  • 解析内容以从数据中查找和提取值-config-url
    属性

  • 导航到data-config-url并将内容加载为JSON对象:
    $ video = json_decode($ this-> getRemoteContent($ video-> getAttribute('data-config-url')));

  • 返回$ video-> request-> files-> h264-> sd-> url - 这将返回一个
    直接链接的SD质量视频。



这是我简单的类,那

  class VideoController 
{

/ **
* @var数组Vimeo视频质量优先级
* /
public $ vimeoQualityPrioritet = array('sd','hd','mobile');

/ **
* @var string Vimeo视频编解码优先级
* /
public $ vimeoVideoCodec ='h264';

/ **
*获取Vimeo视频文件的直接URL
*
* @param string $ url到Vimeo上的视频
* @return string文件URL
* /
public function getVimeoDirectUrl($ url)
{
$ result ='';
$ videoInfo = $ this-> getVimeoVideoInfo($ url);
if($ videoInfo&& $ videoObject = $ this-> getVimeoQualityVideo($ videoInfo-> request-> files))
{
$ result = $ videoObject-> ;网址;
}
return $ result;
}

/ **
*获取Vimeo视频信息
*
* @param string $ url到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.0encoding =UTF-8?>'。\\\
$ 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;
}

/ **
*获取vimeo视频对象
*
* @param stdClass $文件Vimeo文件对象
* @返回stdClass视频文件对象
* /
public function getVimeoQualityVideo($ files)
{
$ video = null;
if(!property_exists($ files,$ this-> vimeoVideoCodec)&& count($ files-> codecs)
{
$ this-> vimeoVideoCodec = array_shift ($文件 - >编解码器);
}
$ codecFiles = $ files-> {$ this-> vimeoVideoCodec};
foreach($ this-> vimeoQualityPrioritet as $ quality)
{
if(property_exists($ codecFiles,$ quality))
{
$ video = $ codecFiles - > {$质量};
break;
}
}
if(!$ video)
{
foreach(get_object_vars($ codecFiles)as $ file)
{
$ video = $ file;
break;
}
}
return $ video;
}

/ **
*通过URL获取远程内容
*
* @param string $ url remote page URL
* @返回字符串结果内容
* /
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;
}

}

使用:

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


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

When you go on the page, Vimeo generates a signature associated with the current timestamp and this video. This information is stored in a JavaScript variable, around line 520 just 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 opened http://vimeo.com/22439234, this returns the HTTP code 200 with an error message.

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

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

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

Why ?

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

With PHP, I use the function 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));
    }

解决方案

The algorithm looks like this:

  • Input data: vimeoUrl.
  • content = getRemoteContent(vimeoUrl).
  • Parse content to find and extract the value from data-config-url attribute.
  • Navigate to data-config-url and load the content as 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 working for this moment.

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'));

这篇关于从PHP中获取Vimeo的直接链接视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆