Comments (1)
感谢大佬~
本人改写了个php版本
也分享下:
kuwolrc.php
<?php
include("./libs.php");
// 定义strToHexBuffer函数,接受一个字符串参数
$get_id = 0;
if (empty($_GET['id'])) {
http_response_code(403);
echo '{"code":403,"msg":"Wrong params."}';
return;
}
$get_id = $_GET['id'];
$bufkey = strToHexBuffer('yeelion');
$bufkeylen = count($bufkey);
// 调用函数,得到返回值(16进制的buffer数组)
function buildParams($id, $isGetLyrics = true)
{
$params = "user=1,web,web,web&requester=localhost&req=1&rid=MUSIC_$id";
if ($isGetLyrics) $params .= '&lrcx=1';
$bufstr = strToHexBuffer($params);
$bufstrlen = count($bufstr);
$output = $bufstr;
$bufkeylen = $GLOBALS['bufkeylen'];
$bufkey = $GLOBALS['bufkey'];
for ($i = 0; $i < $bufstrlen; $i++) {
$output[$i] = 0;
}
$i = 0;
while ($i < $bufstrlen) {
$j = 0;
while ($j < $bufkeylen && $i < $bufstrlen) {
$output[$i] = $bufkey[$j] ^ $bufstr[$i];
$i++;
$j++;
}
}
//print_r($output);
$string = implode($output);
// 将字符串用base64方式编码
$encoded = base64_encode($string);
return $encoded;
}
function decodeLyrics($base64, $isGetLyrics = true)
{
$str = ($base64);
// echo $str;
// echo (substr($str,0,10) == "tp=content")?"true":"false";
if (substr($str, 0, 10) != 'tp=content') return false;
// $buf = strToHexBuffer($str);
$newstr = substr($str, strpos($str, "\r\n\r\n") + 4);
// echo $newstr;
$unzipstr = base64_decode(gzuncompress($newstr));
$bufStr = strToHexBuffer($unzipstr);
$bufStrlen = count($bufStr);
$output = $bufStr;
$i = 0;
$bufkeylen = $GLOBALS['bufkeylen'];
$bufkey = $GLOBALS['bufkey'];
while($i< $bufStrlen){
$j = 0;
while($j<$bufkeylen && $i<$bufStrlen){
$output[$i] = $bufStr[$i] ^ $bufkey[$j];
$i++;
$j++;
}
}
$result = implode($output);
return detect_encoding($result,'utf8');
// return true;
}
/**
* @ string 需要转换的文字
* @ encoding 目标编码
**/
function detect_encoding($string, $encoding = 'gbk')
{
$is_utf8 = preg_match('%^(?:[\x09\x0A\x0D\x20-\x7E]| [\xC2-\xDF][\x80-\xBF]| \xE0[\xA0-\xBF][\x80-\xBF] | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} | \xED[\x80-\x9F][\x80-\xBF] | \xF0[\x90-\xBF][\x80-\xBF]{2} | [\xF1-\xF3][\x80-\xBF]{3} | \xF4[\x80-\x8F][\x80-\xBF]{2} )*$%xs', $string);
if ($is_utf8 && $encoding == 'utf8') {
return $string;
} elseif ($is_utf8) {
return mb_convert_encoding($string, $encoding, "UTF-8");
} else {
return mb_convert_encoding($string, $encoding, 'gbk,gb2312,big5');
}
}
$url = "http://newlyric.kuwo.cn/newlyric.lrc?" . buildParams($get_id);
//echo $url;
$data = fetchURL($url, false);
if ($data == false) {
http_response_code(500);
echo '{"msg":"获取失败 (Step#1)","code":500}';
}
$result = decodeLyrics($data);
if ($result == false) {
http_response_code(500);
echo '{"msg":"获取失败 (Step#2)","code":500}';
}
echo $result;
libs.php
function strToHexBuffer($str)
{
// 将字符串转换为字节数组
$bytes = unpack("C*", $str);
// 定义输出数组,用于存储16进制的buffer元素
$output = array();
// 循环遍历字节数组,将每个字节转换为16进制,并添加到输出数组中
foreach ($bytes as $byte) {
// 使用sprintf函数格式化16进制,并在前面补0(如果需要)
$hex = sprintf("%02x", $byte);
// 使用pack函数将16进制转换为二进制,并添加到输出数组中
$output[] = pack("H*", $hex);
}
// 返回输出数组
return $output;
}
function fetchURL($url, $ispost = false, $postcontent = "")
{
//echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cache-Control: no-cache"));
curl_setopt($ch, CURLOPT_COOKIE, "NMTID=00OTrNMnhnWaFznwESKkN7usch8O14AAAGDKs_klA;");
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postcontent);
}
$output = curl_exec($ch);
// echo json_encode(curl_getinfo($ch));
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($output === false) {
if (in_array(intval(curl_errno($ch)), [7, 28], true)) {
echo '{"success":"fail","msg":"连接超时,请重试。","code":3}';
return false;
//超时的处理代码
} else if (in_array(intval(curl_errno($ch)), [3], true)) {
echo '{"success":"fail","msg":"C++ CURL 不支持的 URL:' . $url . '","code":4}';
} else {
echo '{"success":"fail","msg":"[' . curl_errno($ch) . '] ' . curl_strerror(curl_errno($ch)) . '; ' . curl_error($ch) . '","code":4}';
return false;
}
}
if ($httpCode >= 400 && $httpCode < 404) {
echo '{"success":"fail","msg":"无法访问该文件,请联系站点管理员。","code":5}';
return false;
} else if ($httpCode >= 404 && $httpCode < 500) {
echo '{"success":"fail","msg":"服务器无法找到文件。","code":5}';
return false;
} else if ($httpCode >= 500) {
echo '{"success":"fail","msg":"服务器发生错误,请稍后重试。(Http Status:' . $httpCode . ')","code":6}';
return false;
}
curl_close($ch);
return $output;
}
from kuwomusicapi.
Related Issues (20)
- 请求接口的时候会随机崩溃 HOT 6
- 请求接口的时候会随机崩溃
- music/results HOT 2
- 播放接口参数错误~ HOT 2
- 可以适配一下vercel吗
- Releases?
- 无法获取部分音乐播放链接 HOT 6
- 修复音乐不能播放的问题 HOT 6
- Internal Server Error, real status: 500 HOT 3
- 关于日志文件无法自动删除问题 HOT 20
- 搜索接口参数过时 HOT 7
- 搜索接口请求所需Header再次发生变化 HOT 83
- 尝试修复一点bug HOT 34
- 搜索接口不能使用了 HOT 15
- 提交一个新的搜索API
- debian10要装什么版本的node和npm HOT 2
- 好像不能获取vip歌曲了 HOT 10
- The request is illegal
- banner图视乎不行了
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kuwomusicapi.