PHP获取各大视频网站页面中的Flash播放地址
先看一个简单的,我用PHP实现了这个功能,我觉得用PHP来做这项工作简直是一种享受!使用其提供的强大的HTML页面处理函数和正则表达式,短短的几行代码就能搞定这个功能。
贴一下关键代码:
- <?php
-
- function get_flash_url( $url )
- {
- $lines = file($url);
- foreach ($lines as $linenum=> $line) {
- preg_match_all('|<input type="text" id="link2" value="([^<>]+)" />|',$line,$result);
- $swfurl=$result[1][0];
- if(!emptyempty($swfurl))
- return $swfurl;
- }
- }
- ?>
- <?php
- $url=$_SERVER["QUERY_STRING"];
-
- $flashurl= get_flash_url($url);
-
- echo ( $flashurl );
-
- ?>
比如这个文件我们存为 test.php,那么我们只需要运行test.php?优酷视频的url 就可以解析出FLASH地址了.
思路很简单,就是先看看优酷视频网页的HTML代码里关键FLASH地址那段的特征,随便找个网页,比如我们可以看到这一段:
- <div class="item"><span class="label">flash地址: </span> <input type="text" id="link2" value="http://player.youku.com/player.php/sid/XMTU1MzcxMzAw/v.swf" />
然后使用正则表达式来将其中的地址段匹配掉,就OK了,上面只是单个的,后来找到一个升级的方法,可以自动获取各大视频网站flash视频播放地址,目前已支持新浪播客、优酷网、土豆网、酷6网、搜狐视频、56网、奇艺网、凤凰网等视频网站的视频播放页链接。
代码如下:
- <?php
- if (!emptyempty($_GET['url']))
- {
- $web_video = new free_flash_video();
- $web_video->index();
- }
-
-
-
-
-
-
- class free_flash_video{
-
- public function index()
- {
-
- $url = $_GET['url'];
- if ($url)
- {
- $parse = parse_url($url);
-
- isset($parse['host']) && $host = $parse['host'];
-
- $methods = array(
- "www.tudou.com" => "tudou",
- "v.youku.com" => "youku",
- "v.ku6.com" => "ku6",
- "tv.sohu.com" => "sohu",
- "video.sina.com.cn" => "sina",
- "www.56.com" => "five_six",
- "www.iqiyi.com" => "iqiyi",
- "v.ifeng.com" => "ifeng",
- "www.yinyuetai.com" => "yinyuetai",
- );
-
- isset($methods[$host]) && print_r($this->$methods[$host]($url));
-
- }
- }
-
-
-
-
-
-
- private function youku($url)
- {
- preg_match('/id_(.*).html/', $url,$url);
-
- if (isset($url[1]))
- {
- return "http://static.youku.com/v/swf/qplayer.swf?VideoIDS={$url[1]}&=&isAutoPlay=true&embedid";
- }
- }
-
-
-
-
-
-
- private function tudou($url)
- {
- $data = file_get_contents($url);
-
-
- preg_match('/iid:(.*)/', $data, $result);
- if (isset($result[1]))
- {
- $url = trim($result[1]);
- return "http://www.tudou.com/player/skin/plu.swf?iid={$url}";
- }
- }
-
-
-
-
-
-
- private function ku6($url)
- {
-
- preg_match('/show/(.*).{1}/', $url, $result);
-
- if (isset($result[1]))
- {
- return "http://player.ku6.com/refer/{$result[1]}/v.swf&auto=1";
- }
- }
-
-
-
-
-
-
- private function sohu($url)
- {
- $data = file_get_contents($url);
-
- preg_match('/<meta property="og:video" content="(.*)"/>/', $data, $result);
- if (isset($result[1]))
- {
- return $result[1];
- }
- }
-
-
-
-
-
-
- private function sina($url)
- {
- $data = file_get_contents($url);
-
- preg_match("/swfOutsideUrl:'(.*)',/", $data, $result);
- if (isset($result[1]))
- {
- return $result[1];
- }
- }
-
-
-
-
-
-
- private function five_six($url)
- {
-
- preg_match('/(v_.*).html/', $url, $result);
-
- if (isset($result[1]))
- {
- return "http://player.56.com/{$result[1]}.swf";
- }
- }
-
-
-
-
-
-
- private function iqiyi($url)
- {
- $data = file_get_contents($url);
-
-
- preg_match('/("videoId":"(.*)")|(data-player-videoid="(.*)")/U', $data, $result);
-
- if (isset($result[4]))
- {
- return "http://www.iqiyi.com/player/20130315154043/SharePlayer.swf?vid={$result[4]}";
- }
- }
-
-
-
-
-
-
- private function ifeng($url)
- {
-
- preg_match('/d+/(.*)./', $url, $result);
-
- if (isset($result[1]))
- {
- return "http://v.ifeng.com/include/exterior.swf?guid={$result[1]}&fromweb=sinaweibo&AutoPlay=true";
- }
- }
- }
- ?>
PHP API调用实例
- /tools/web_video.php?url=视频页面地址
- eg:/web_video.php?url=http: