好吧之前写得那个网易云音乐下载现在不行了,重新查看了一下,原来是链接被重定向了
重定向
稍微修改一下代码就ok了:
<html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>网易云音乐下载--止语</title> <link rel="shortcut icon" href="//s1.music.126.net/style/favicon.ico?v20180823"> <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script> <script src="./jquery-1.11.3.min.js"></script> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <style> .container-fluid{ background:#E5E7E9; } .container{ height:35px; line-height:35px; } .y_title{ font-size:25px; } .idwt{ height:20px; line-height:20px; } p a{ font-size:30px; } @media only screen and (max-width:768px) { .bj { min-height:600px; background:url(./bj.jpg); background-repeat:no-repeat; background-size:768px; } } @media only screen and (min-width:768px) and (max-width:992px) { .bj { min-height:1200px; background:url(./bj.jpg); background-repeat:no-repeat; background-size:992px; } } @media only screen and (min-width:992px) and (max-width:9999px) { .bj { min-height:1200px; background:url(./bj.jpg); background-size:1920px 1200px; } } /*.bj{ background:url(./bj.jpg); background-size:100% 100%; }*/ </style> </head> <body> <div class="container-fluid"> <div class="container"> <div class="row"> <div class="col-md-3 y_title">网易云音乐下载</div> </div> </div> </div> <div class="row bj"> <div class="col-md-4 col-md-offset-4"> <form action="?mod=xz" method="post"> <div class="form-group"> <label for="exampleInputEmail1">音乐ID</label> <input type="text" class="form-control" name="id" id="exampleInputEmail1" placeholder="音乐ID"> </div> <button type="submit" class="btn btn-info btn-lg btn-block">下载</button> </form> <p> <?php $yy = $_GET['mod']; if($yy == 'xz'){ $id = $_POST['id']; if(!empty($id)){ if(is_numeric($id) && strlen($id) > 4){ $file = "./music/".$id.".mp3"; $music_name = "".$id.".mp3"; if(!file_exists($file)){ $url = "https://music.163.com/song/media/outer/url?id=".$id.".mp3"; $url = getrealurl($url); if($url == "http://music.163.com/404") { echo"<script>alert('无ID:".$id."的歌曲,请检查ID是否正确')</script>"; return true; } $mp3 = file_get_contents($url); if(!empty($mp3) && !is_null(strlen($mp3))){ $fp = fopen($file, "w"); $cg = fwrite($fp,$mp3); fclose($fp); if(!empty($cg)){ echo"<a href='".$file."' download='".$music_name."'>资源准备好啦,戳我下载吧</a>"; } }else{ echo"<script>alert('无ID:".$id."的歌曲,请检查ID是否正确')</script>"; } }else{ echo"<a href='".$file."' download='".$music_name."'>资源准备好啦,戳我下载吧</a>"; } }else{ echo "<script>alert('请输入正确的ID')</script>"; } }else{ echo"<script>alert('ID不能为空')</script>"; } } function getrealurl($url,$postData = array(), $options = array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER,$options); // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文 curl_setopt($ch, CURLOPT_HEADER, true); // // 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文 curl_setopt($ch, CURLOPT_NOBODY, true); // 使用上面定义的 ua // curl_setopt($ch, CURLOPT_USERAGENT,$user_agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 不同的点 开始 为 false curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数 //https请求 不验证证书和host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不同的点 结束 $data = curl_exec($ch); curl_close($ch); preg_match('/location:\s+(.*?)\s+/is',$data,$urlData); return $urlData[1]; } ?> </p> <blockquote class="bs-callout-info"> 关于ID,请前往<a href="http://music.163.com" target="_blank">http://music.163.com</a>获取 </blockquote> <button type="button" class="btn btn-success btn-block" data-toggle="modal" data-target=".bs-example-modal-lg">点击查看获取歌曲ID的方法</button> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <h4 class="text-center" style="font-weight:bold;">获取ID方法</h4><hr/> <p class="" style="font-size:20px;">前往网易云音乐(<a href="http://music.163.com" target="_blank">http://music.163.com</a>)搜索想要下载的歌曲,进入歌曲页面,在地址栏查看歌曲的ID,复制过来就好啦~</p> <img src="./wyy.png" alt="id示例" width="100%"/> </div> </div> </div> </div> </div> </body> <script> var aBtn = document.getElementsByTagName("button")[0] var aP = document.getElementsByTagName("p")[0] aBtn.onclick = function(){ aP.innerHTML = "<span class='glyphicon glyphicon-cd'>正在准备资源,请稍等....</span>" } </script> </html>