Good things are coming again. Get Youku's real video address through the Youku Link

Source: Internet
Author: User
Provides a variety of official and user-released code examples, code reference, you are welcome to exchange learning good things again, through the Youku link to get Youku real video address, you can take a friend to study, tested
For more technical articles, see
Http://www.dahuzhi.com

/*
Return Value:
Array
'Mp4' => string 'HTTP: // response? K = 48daa69002023a2a2828f4f1 '(length = 167)
'Flv' => string 'HTTP: // response? K = b9c1dfedde4eb5052828f4f1 '(length = 167)
*/

If (YoukuFlv: getYoukuFlv ("http://v.youku.com/v_show/id_XNjEyOTE4NTEy_ev_1.html "))
{
Var_dump (YoukuFlv: result ());
}
Else
{
Echo YoukuFlv: error ();
}



/*
Obtain the youku video address
*/
Class YoukuFlv {

Static private $ error = "";
Static private $ result = array ();

Static public function getYoukuFlv ($ url ){
// Obtain the youkuid from the url
If (! $ Id = self: getYoukuId ($ url )){
Return false;
}
// Obtain the youku video details
$ Content = self: get_curl_contents ("http://v.youku.com/player/getPlayList/VideoIDS/". $ id );
$ Data = json_decode ($ content );
If (! Isset ($ data-> data [0]-> streamfileids )){
Self: $ error = "Cannot find this video ";
Return false;
}
Foreach ($ data-> data [0]-> streamfileids AS $ k => $ v ){
If ($ k = 'flv' | $ k = 'mp4 '){
// Sid
$ Sid = self: getSid ();
// Fileid
$ Fileid = self: getfileid ($ v, $ data-> data [0]-> seed );
$ One = ($ data-> data [0]-> segs-> $ k );
Self: $ result [$ k] = "condition }? K = {$ one [0]-> k }";
}
}
If (empty (self: $ result )){
Self: $ error = "this viod is not in MP4 or flv format ";
Return false;
} Else {
Return true;
}
}
Static public function error (){
Return self: $ error;
}

Static public function result (){
Return self: $ result;
}

Static private function getYoukuId ($ url ){
// Url cannot be blank
If ($ url = "" | substr ($ url, 0, 29 )! = "Http://v.youku.com/v_show/id "){
Self: $ error = "url is error ";
Return false;
}
Return substr ($ url, 29,-5 );
}

Static private function get_curl_contents ($ url, $ second = 5 ){
If (! Function_exists ('curl _ init ') die ('php. ini has not enabled php_curl.dll ');
$ C = curl_init ();
Curl_setopt ($ c, CURLOPT_URL, $ url );
$ UserAgent = $ _ SERVER ['HTTP _ USER_AGENT '];
Curl_setopt ($ c, CURLOPT_USERAGENT, $ UserAgent );
Curl_setopt ($ c, CURLOPT_HEADER, 0 );
Curl_setopt ($ c, CURLOPT_TIMEOUT, $ second );
Curl_setopt ($ c, CURLOPT_RETURNTRANSFER, true );
$ Cnt = curl_exec ($ c );
Curl_close ($ c );
Return $ cnt;
}
Static private function getSid (){
$ Sid = time (). (rand (10000) + );
Return $ sid;
}
Static private function getfileid ($ fileId, $ seed ){
$ Mixed = self: getMixString ($ seed );
$ Ids = explode ("*", $ fileId );
Unset ($ ids [count ($ ids)-1]);
$ RealId = "";
For ($ I = 0; $ I <count ($ ids); ++ $ I ){
$ Idx = $ ids [$ I];
$ RealId. = substr ($ mixed, $ idx, 1 );
}
Return $ realId;
}
Static private function getMixString ($ seed ){
$ Mixed = "";
$ Source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:. _-1234567890 ";
$ Len = strlen ($ source );
For ($ I = 0; $ I <$ len; ++ $ I ){
$ Seed = ($ seed * 211 + 30031) % 65536;
$ Index = ($ seed/65536 * strlen ($ source ));
$ C = substr ($ source, $ index, 1 );
$ Mixed. = $ c;
$ Source = str_replace ($ c, "", $ source );
}
Return $ mixed;
}
}




?>

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.