Home  >  Article  >  Backend Development  >  How to get the final address of youku video in php

How to get the final address of youku video in php

WBOY
WBOYOriginal
2016-07-25 08:56:201523browse
  1. /**

  2. * Get the final address of youku video
  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 < count($ids);++$i) {
  75. $idx = $ids[$i];
  76. $realId .= substr($mixed,$idx,1);
  77. }
  78. return $realId;
  79. }
  80. static private function getMixString($seed) {
  81. $mixed = "";
  82. $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890";
  83. $len = strlen($source);
  84. for($i=0;$i< $len;++$i){
  85. $seed = ($seed * 211 + 30031) % 65536;
  86. $index = ($seed / 65536 * strlen($source));
  87. $c = substr($source,$index,1);
  88. $mixed .= $c;
  89. $source = str_replace($c, "",$source);
  90. }
  91. return $mixed;
  92. }
  93. }
  94. if(YoukuFlv::getYoukuFlv("http://v.youku.com/v_show/id_XNjEyOTE4NTEy_ev_1.html")){
  95. print_r( YoukuFlv::result() );
  96. }else{
  97. echo YoukuFlv::error();
  98. }

  99. ?>

复制代码

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



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn