Get direct link videos from Vimeo in PHP

后端 未结 2 922
你的背包
你的背包 2021-02-06 04:30

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 vi

2条回答
  •  孤独总比滥情好
    2021-02-06 05:14

    Try add a valid user-agent to headers for an each request. For this you must use cURL or HttpRequest instead file_get_contents().

    After such manipulations I got a working link for downloading the video file.

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

提交回复
热议问题