PHP method for obtaining the real flv file address of a youku video

Source: Internet
Author: User
Tags flv file
This article mainly introduces how to obtain the real flv file address of a youku video by using PHP. It can be used to parse the real address of a youku video. For more information, see

This article mainly introduces how to obtain the real flv file address of a youku video by using PHP. It can be used to parse the real address of a youku video. For more information, see

This example describes how to use PHP to obtain the actual flv file address of a youku video. Share it with you for your reference. The specific analysis is as follows:

A Webmaster asked me to help him develop a real flv address that can automatically test the youku video website. Below I sorted it out and solved this problem very well. You can refer to it.

The Code is as follows:

The Code is as follows:

<? Php
$ Videourl = 'HTTP: // v.youku.com/v_show/id_xmja5mjq0otq0.html ';
Function get_content ($ url, $ data ){
If (is_array ($ data )){
$ Data = http_build_query ($ data ,'','&');
}
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
$ Result = curl_exec ($ ch );
Return $ result;
}
$ Str = get_content ('HTTP: // developer.pengyou.com/json.php? Mod = usershare & act = geturlinfo ', array ('url' => $ videourl ));
$ Str = json_decode ($ str );
Var_dump ($ str );
?>


This test shows that the swf file is not the flv file we need. Later, we improved the method by referring to a webmaster. The core code is as follows:

The Code is as follows:

<? Php
Function fetch_youku_flv ($ url ){
Preg_match ("# id _(.*?). Html # ", $ url, $ out );
$ Id = $ out [1];
$ Content = get_curl_contents ('HTTP: // v.youku.com/player/getPlayList/VideoIDS/'.$id );
$ Data = json_decode ($ content );
Foreach ($ data-> data [0]-> streamfileids AS $ k => $ v ){
$ Sid = getSid ();
$ Fileid = getfileid ($ v, $ data-> data [0]-> seed );
$ One = ($ data-> data [0]-> segs-> $ k );
If ($ k = 'flv' | $ k = 'mp4') return "response }? K = {$ one [0]-> k }";
Continue;
}
}
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 );
$ Cnt = mb_check_encoding ($ cnt, 'utf-8 ')? Iconv ('gbk', 'utf-8 // IGNORE ', $ cnt): $ cnt; // character encoding conversion
Curl_close ($ c );
Return $ cnt;
}
Function getSid (){
$ Sid = time (). (rand (10000) + );
Return $ sid;
}
Function getkey ($ key1, $ key2 ){
$ A = hexdec ($ key1 );
$ B = $ a ^ 0xA55AA5A5;
$ B = dechex ($ B );
Return $ key2. $ B;
}
Function getfileid ($ fileId, $ seed ){
$ Mixed = 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;
}
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;
}
?>


The Code is as follows:

The Code is as follows:

$ Url = 'HTTP: // v.youku.com/v_show/id_xmzg2otq3mjq1_html ';
Echo fetch_youku_flv ($ url );

Access:

The Code is as follows:

? Url = http://v.youku.com/v_show/id_XMzg2OTQ3MjQw.html

The output result is:

The Code is as follows:

? K = c2a4327df2bb5a65261d40ea

This can be downloaded directly.

I hope this article will help you with php programming.

,

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.