7月122022
PHP酷狗音乐采集类
php /** * 酷狗API类 * @author Flc <2015-08-27 22:51:24> */ $kugou =new kugou(); $lists = $kugou->getList('儿歌'); print_r($lists); $music = $kugou->getMusic('2dc67062d830de3b60b7e4b09820e062'); print_r($music); print_r($music['url']); class kugou{ const LIST_URL ='http://lib9.service.kugou.com/websearch/index.php?page={page}&keyword={keyword}&cmd=100&pagesize={pagesize}'; const MUSIC_URL ='http://m.kugou.com/app/i/getSongInfo.php?hash={hash}&cmd=playInfo'; protectedstatic $http =null; /** * 初始化 */ function __construct(){ if(self::$http ==null)self::$http =new http(); } /** * 获取列表 * @param string $keyword 关键字 * @param integer $page 当前页 * @param integer $pagesize 每页获取数量 * @return array|false */ publicfunction getList($keyword, $page =1, $pagesize =10){ $url = str_replace(array('{page}','{keyword}','{pagesize}'), array($page, $keyword, $pagesize),self::LIST_URL); $json =self::$http->http_gets($url); if(!$json)returnfalse; $resp = json_decode($json,true); return $resp; } /** * 获取音乐地址 * @param string $hash 音乐hash码 * @return string|false */ publicfunction getMusic($hash){ $url = str_replace(array('{hash}'), array($hash),self::MUSIC_URL); $json =self::$http->http_gets($url); if(!$json)returnfalse; $resp = json_decode($json,true); return $resp; } } /** * http请求类 * @author Flc <2015-08-27 22:51:47> */ class http{ /** * 服务器通过get请求获得内容 * @param string $baseURL 基础URL * @param array $keysArr 请求参数 * @return string [description] */ publicstaticfunction http_gets($baseURL, $keysArr = array()){ $url =self::combineURL($baseURL, $keysArr); $ch = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSLVERSION,1);//CURL_SSLVERSION_TLSv1 } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $sContent = curl_exec($ch); $aStatus = curl_getinfo($ch); curl_close($ch); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ returnfalse; } } /** * post方式请求资源 * @param string $url 基于的baseUrl * @param array $keysArr 请求的参数列表 * @return string 返回的资源内容 */ publicstaticfunction http_post($url, $keysArr = array()){ $ch = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch, CURLOPT_SSLVERSION,1);//CURL_SSLVERSION_TLSv1 } $aPOST = array(); foreach($keysArr as $key=>$val){ $aPOST[]= $key."=".urlencode($val); } $strPOST = implode("&", $aPOST); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$strPOST); $sContent = curl_exec($ch); $aStatus = curl_getinfo($ch); curl_close($ch); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ returnfalse; } } /** * combineURL * 拼接url * @param string $baseURL 基于的url * @param array $keysArr 参数列表数组 * @return string 返回拼接的url */ publicstaticfunction combineURL($baseURL, $keysArr){ if(empty($keysArr)||!is_array($keysArr))return $baseURL; $combined = $baseURL."?"; $valueArr = array(); foreach($keysArr as $key => $val){ $valueArr[]="$key=".urlencode($val); } $keyStr = implode("&",$valueArr); $combined .=($keyStr); return $combined; } }
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!