PHP代码转C#代码 哪儿出错了?
获取优酷的真实地址。<?php
//--调用方法http://xiaomizhou.net/demo/flv.php?url=http://v.youku.com/v_show/id_XMzg2OTQ3MjQw.html
echo fetch_youku_flv($_GET['url']);
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 "http://f.youku.com/player/getFlvPath/sid/{$sid}_00/st/{$k}/fileid/{$fileid}?K={$one[0]->k}";
continue;
}
}
function get_curl_contents($url, $second = 5){
if(!function_exists('curl_init')) die('php.ini未开启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; //字符编码转换
curl_close($c);
return $cnt;
}
function getSid() {
$sid = time().(rand(0,9000)+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;
}
这个PHP版本的 现在可用。
http://xiaomizhou.net/demo/flv.php?url=http://v.youku.com/v_show/id_XMzg2OTQ3MjQw.html
生成后的地址可播放下载。
求翻译C#版本的 我自己翻译之后不能使用。
PHP C# --------------------编程问答-------------------- 除
string type = "";
//获取Mp4
public string GetMp4()
{
//http://v.youku.com/v_show/XMzg2OTQ3MjQw.html
HttpHelper ht = new HttpHelper();
string input = ht.GetHtml("http://v.youku.com/player/getPlayList/VideoIDS/XMzg2OTQ3MjQw");//获取网页源代码
string sid = genSid();
double seed = genSend(input);
string ykfileid = GetFileID(input);
string fileid = getFileID(ykfileid, seed);
string key1 = GetK1(input);
string key2 = GetK2(input);
string key = genKey(key1, key2);
string youku易做图 = "http://f.youku.com/player/getFlvPath/sid/" + sid + "_00/st/" + type + "/fileid/" + fileid + "?K=" + key;
return youku易做图;
}
#region 获取优酷的基本数据
private double genSend(string input)
{
string strreg = "seed\":(?<seed>.*?),";
double seed = 0;
Regex reg = new Regex(strreg, RegexOptions.IgnoreCase);
foreach (Match item2 in reg.Matches(input))
{
seed = Convert.ToDouble(item2.Groups["seed"].Value);
}
return seed;
}
public string GetK1(string input)
{
string strreg = "key1\":\"(?<seed>.*?)\",";
string key1 = "";
Regex reg = new Regex(strreg, RegexOptions.IgnoreCase);
foreach (Match item2 in reg.Matches(input))
{
key1 = item2.Groups["seed"].Value;
}
return key1;
}
public string GetK2(string input)
{
string strreg = "key2\":\"(?<seed>.*?)\",";
string key2 = "";
Regex reg = new Regex(strreg, RegexOptions.IgnoreCase);
foreach (Match item2 in reg.Matches(input))
{
key2 = item2.Groups["seed"].Value;
}
return key2;
}
private string GetFileID(string input)
{
string strreg = "streamfileids\":{(?<streamfileids>.*?)}";
string fileids = "";
string fileid = "";
Regex reg = new Regex(strreg, RegexOptions.IgnoreCase);
foreach (Match item2 in reg.Matches(input))
{
fileids = item2.Groups["streamfileids"].Value;
}
////mp4优先
//if (fileids.IndexOf("mp4") >= 0)
//{
// type = "mp4";
// strreg = "mp4\":\"(?<fileid>.*?)\"";
// reg = new Regex(strreg, RegexOptions.IgnoreCase);
// foreach (Match item2 in reg.Matches(fileids))
// {
// fileid = item2.Groups["fileid"].Value;
// }
//}//flv其次
if (fileids.IndexOf("flv") >= 0)
{
type = "flv";
strreg = "flv\":\"(?<fileid>.*?)\"";
reg = new Regex(strreg, RegexOptions.IgnoreCase);
foreach (Match item2 in reg.Matches(fileids))
{
fileid = item2.Groups["fileid"].Value;
}
}
return fileid;
}
#endregion
#region 解析优酷真实地址方法
private string genSid()
{
int i1 = (int)(1000 + Math.Floor((double)(new Random().Next(999))));
int i2 = (int)(1000 + Math.Floor((double)(new Random().Next(9000))));
TimeSpan ts = new TimeSpan(System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
return Convert.ToInt64(ts.TotalMilliseconds).ToString() + "" + i1 + "" + i2;
}
private string getFileID(string fileid, double seed)
{
string mixed = getFileIDMixString(seed);
string[] ids = fileid.Split('*');
StringBuilder realId = new StringBuilder();
int idx;
for (int i = 0; i < ids.Length - 1; i++)
{
idx = int.Parse(ids[i]);
realId.Append(mixed[idx]);
}
return realId.ToString();
}
private string getFileIDMixString(double seed)
{
StringBuilder mixed = new StringBuilder();
StringBuilder source = new StringBuilder("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890");
int index, len = source.Length;
for (int i = 0; i < len; ++i)
{
seed = (seed * 211 + 30031) % 65536;
index = (int)Math.Floor(seed / 65536 * source.Length);
mixed.Append(source[index]);
source.Remove(index, 1);
}
return mixed.ToString();
}
private string genKey(string key1, string key2)
{
int key = Convert.ToInt32(key1, 16);
var tempkey = key ^ 0xA55AA5A5;
return key2 + Convert.ToString(tempkey, 16).Substring(8);
}
#endregion
补充:.NET技术 , C#