How PHP Gets the Youku video final address

Source: Internet
Author: User
  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. Get Youkuid from URL
  11. if (! $id = Self::getyoukuid ($url)) {
  12. return false;
  13. }
  14. Get Youku Video Details
  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 was 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. The URL cannot be empty
  46. if ($url = = "" | | substr ($url, 0,)! = "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 not turned on 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. ?>

Copy Code

Another way to call: http://player.youku.com/player.php/sid/xmji0mdiwndc2/v.swfhttp://player.youku.com/player.php/sid/{$id }/v.swf

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.