微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从PHP获取Vimeo的直接链接视频

我希望通过 PHP脚本直接链接到Vimeo的视频.
我设法手动找到它们,但我的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

如果此URL未使用打开http://vimeo.com/22439234的IP地址打开,则会返回带有错误消息的HTTP代码200.

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

当我建立这个链接http://player.vimeo.com/play_redirect?…手动(“右键单击”>“源代码”>“520行”)它可以工作.

但是使用PHP和regex,它会返回带有错误消息的HTTP代码200.

为什么?

根据我的观察,Vimeo没有检查http:// player.vimeo.com/play_redirect的HTTP请求的标头?…
GET,HEAD,带饼干,没有cookies,推荐人等……不会改变.

使用PHP,我使用函数file_get_contents()和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',$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).
>解析内容以从data-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 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,CURLOPT_MAXREDirs,CURLOPT_USERAGENT,'spider');
        $content = curl_exec($ch);

        curl_close($ch);

        return $content;
    }

}

使用:

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

原文地址:https://www.jb51.cc/php/136317.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐