首页 > 后端开发 > php教程 > php获取youku视频最终地址的方法

php获取youku视频最终地址的方法

WBOY
发布: 2016-07-25 08:56:20
原创
1585 人浏览过
  1. /**

  2. * 取得youku视频的最终地址
  3. * edit by bbs.it-home.org
  4. * at: 2013-10-2
  5. */
  6. class YoukuFlv{
  7. static private $error = "";
  8. static private $result = array();
  9. static public function getYoukuFlv($url){
  10. //从url获取youkuid
  11. if(! $id = self::getYoukuId($url)){
  12. return false;
  13. }
  14. //获取youku视频详细信息
  15. $content = self::get_curl_contents( "http://v.youku.com/player/getPlayList/VideoIDS/".$id );
  16. $data = json_decode($content);
  17. if(!isset($data->data[0]->streamfileids)){
  18. self::$error = "Cannot find this video";
  19. return false;
  20. }
  21. foreach($data->data[0]->streamfileids AS $k=>$v){
  22. if($k == 'flv' || $k == 'mp4'){
  23. //sid
  24. $sid= self::getSid();
  25. //fileid
  26. $fileid = self::getfileid($v,$data->data[0]->seed);
  27. $one=($data->data[0]->segs->$k);
  28. self::$result[$k] = "http://f.youku.com/player/getFlvPath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?K={$one[0]->k}";
  29. }
  30. }
  31. if(empty(self::$result)){
  32. self::$error = "THIS VIOD IS NOT IN MP4 OR FLV FORMAT";
  33. return false;
  34. }else{
  35. return true;
  36. }
  37. }
  38. static public function error(){
  39. return self::$error;
  40. }
  41. static public function result(){
  42. return self::$result;
  43. }
  44. static private function getYoukuId($url){
  45. //url 不能为空
  46. if($url == "" || substr($url , 0 , 29) != "http://v.youku.com/v_show/id_"){
  47. self::$error = "URL IS ERROR";
  48. return false;
  49. }
  50. return substr($url , 29 , -5);
  51. }
  52. static private function get_curl_contents($url, $second = 5){
  53. if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
  54. $c = curl_init();
  55. curl_setopt($c,CURLOPT_URL,$url);
  56. $UserAgent=$_SERVER['HTTP_USER_AGENT'];
  57. curl_setopt($c,CURLOPT_USERAGENT,$UserAgent);
  58. curl_setopt($c,CURLOPT_HEADER,0);
  59. curl_setopt($c,CURLOPT_TIMEOUT,$second);
  60. curl_setopt($c,CURLOPT_RETURNTRANSFER, true);
  61. $cnt = curl_exec($c);
  62. curl_close($c);
  63. return $cnt;
  64. }
  65. static private function getSid() {
  66. $sid = time().(rand(0,9000)+10000);
  67. return $sid;
  68. }
  69. static private function getfileid($fileId,$seed) {
  70. $mixed = self::getMixString($seed);
  71. $ids = explode("*",$fileId);
  72. unset($ids[count($ids)-1]);
  73. $realId = "";
  74. for ($i=0;$i $idx = $ids[$i];
  75. $realId .= substr($mixed,$idx,1);
  76. }
  77. return $realId;
  78. }
  79. static private function getMixString($seed) {
  80. $mixed = "";
  81. $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
  82. $len = strlen($source);
  83. for($i=0;$i $seed = ($seed * 211 + 30031) % 65536;
  84. $index = ($seed / 65536 * strlen($source));
  85. $c = substr($source,$index,1);
  86. $mixed .= $c;
  87. $source = str_replace($c, "",$source);
  88. }
  89. return $mixed;
  90. }
  91. }
  92. if(YoukuFlv::getYoukuFlv("http://v.youku.com/v_show/id_XNjEyOTE4NTEy_ev_1.html")){
  93. print_r( YoukuFlv::result() );
  94. }else{
  95. echo YoukuFlv::error();
  96. }
  97. ?>

复制代码

另外一种调用方法: http://player.youku.com/player.php/sid/XMjI0MDIwNDc2/v.swf http://player.youku.com/player.php/sid/{$id}/v.swf



来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板