记录一下使用经验,常规参数封装使用。
API接口版
具体操作类XmlyApi.php
示例,更多需求可前往喜马拉雅官方文档查询,调用方法同理。
<?php
/**
* author : zhw
* 2020-03-03
* 喜马拉雅认证接口
*/
header('Content-Type:application/json; charset=utf-8');
include_once("xmly.php");
class XmlyApi extends Xmly
{
public function __construct($get)
{
$this->get = $get;
}
/**
* 获取分类
*/
public function getCategoriesList()
{
$o_url = "/categories/list";
$parme = array(
'server_api_version' => '1.0.0', //服务器端API版本号
);
$url = $this->createUrl($o_url, $parme);
$res = json_decode(file_get_contents($url), true);
var_dump($res);
}
/**
* 获取专辑
* @param Int $category_id 分类ID,为0时表示热门分类。
* @param Int $calc_dimension 返回结果排序维度:1-最火,2-最新,3-最多播放
* @param Int $page 返回第几页,从1开始,默认为1
* @param Int $count 每页大小,范围为[1,200],默认为20
* @return ArrayObject $res
*/
public function getAlbumsList($category_id = 6, $calc_dimension = 1, $page = 1, $count = 20)
{
$o_url = '/v2/albums/list';
$category_id = $this->get['category_id'] ? $this->get['category_id'] : 6;
$page = $this->get['page'];
$count = $this->get['limit'];
if (empty($page)) {
$page = 1;
}
if (empty($count)) {
$count = 20;
}
$parme = array(
'category_id' => $category_id,
'calc_dimension' => $calc_dimension,
'page' => $page,
'count' => $count,
'server_api_version' => '1.0.0',
);
$url = $this->createUrl($o_url, $parme);
$res = file_get_contents($url);
echo $res;
}
/**
* 获取专辑
* @param Int $ids 以英文逗号分隔的专辑ID,一次最多传200个,超出部分ID会被忽略
* @param Boolen $with_metadata true代表返回metadata,false或不填不返回,获取所有元数据列表,可以使用接口/metadata/list
* @return ArrayObject $res
*/
public function getAlbumsInfo($ids = '29122679,17592887,30589078,245037,', $with_metadata = true)
{
$o_url = '/albums/get_batch';
$parme = array(
'ids' => $ids,
'with_metadata' => $with_metadata,
'server_api_version' => '1.0.0',
);
$url = $this->createUrl($o_url, $parme);
$res = json_decode(file_get_contents($url), true);
var_dump($res);
}
/**
* 获取专辑下声音列表
* @param SInt $album_id 专辑ID
* @param Boolean $sort 返回结果排序方式: "asc" - 喜马拉雅正序,"desc" - 喜马拉雅倒序,"time_asc" - 发布时间升序,"time_desc" - 发布时间降序,默认为"asc"
* @param Int $page 返回第几页,从1开始,默认为1
* @param Int $count 每页大小,范围为[1,200],默认为20
* @return ArrayObject $res
*/
public function getAlbumsBrowseList($album_id = 35505996, $sort = 'asc', $page = 1, $count = 20)
{
$o_url = '/albums/browse';
$album_id = $this->get['album_id'] ? $this->get['album_id'] : 35505996;
$page = $this->get['page'];
$count = $this->get['limit'];
$parme = array(
'album_id' => $album_id,
'sort' => $sort,
'page' => $page,
'count' => $count,
'server_api_version' => '1.0.0',
);
$url = $this->createUrl($o_url, $parme);
$tracks_res = file_get_contents($url);
$tracks_res = json_decode($tracks_res,true);
$ids = '';
foreach($tracks_res['tracks'] as $v){
$ids .= $v['id'].',';
}
$res['tracks_count'] = $tracks_res['total_count'];
$res['tracks'] = $this->getBatchTracksInfo($ids);
echo json_encode($res);
}
/**
* 批量获取声音信息
* @param String $ids 以英文逗号分隔的声音ID,一次最多传200个,超出部分ID会被忽略
* @param Boolean $only_play_info 可选参数,为true时只返回音频播放地址
* @return ArrayObject $res
*/
public function getBatchTracksInfo($ids = '270369642,270372994,270372995,270372996', $only_play_info = true)
{
$o_url = '/tracks/get_batch';
$parme = array(
'ids' => $ids,
'only_play_info' => $only_play_info,
'server_api_version' => '',
);
$url = $this->createUrl($o_url, $parme);
$res = json_decode(file_get_contents($url),true);
return $res;
}
}
$method = $_GET['method'];
$data = $_GET;
if (!$method) {
$method = $_POST['method'];
$data = $_POST;
}
$c = new XmlyApi($data);
if (method_exists($c, $method)) {
$c->$method();
} else {
echo 'cess';
}
父类xmly.php
主要是生成访问链接等公用方法,包含:sig参数的生成。
<?php
/**
* author : zhw
* 2020-03-03
* 喜马拉雅认证接口
*/
header('Content-Type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin:*');
//喜马拉雅开发者后台配置得到参数
define("CLIENT_ID", "2********1");
define("CLIENT_SECRET", "a*****51");
define("CLIENT_STATIC_KEY", "g****P");
class Xmly
{
public function __construct($get)
{
}
/**
* createUrl 生成请求连接
* @param String $url
* @param Array $data
* @return $tmpInfo
*/
public function createUrl($o_url, $data)
{
if (!$data['device_id']) {
$rand = rand(1, 99999);
$data['device_id'] = md5($rand);
}
$url = "https://api.ximalaya.com{$o_url}?";
$rand_only = time() . rand(1, 9999);
$data['app_key'] = CLIENT_ID;
$data['client_os_type'] = 4; //客户端系统类型,固定值
$data['nonce'] = (string) $rand_only; //设备唯一码
$data['timestamp'] = $this->getMillisecond(); //毫秒级别时间戳
//$data['server_api_version'] = '1.0.0'; //服务器端API版本
//生成sign
ksort($data);
$parme = "";
foreach ($data as $k => $v) {
$parme .= "$k=$v&";
}
$parme = trim($parme, '&');
$str = base64_encode($parme);
$hashKey = CLIENT_SECRET . CLIENT_STATIC_KEY;
$sigStr = "&sig=" . md5(hash_hmac('sha1', $str, $hashKey, true));
$url .= $parme . $sigStr;
return $url; //生成的url的timestamp 标签被html页面转译了 如果是用浏览器测试 记得修改一下这个标签
}
/**
* 时间戳 - 精确到毫秒
* @return float
*/
public function getMillisecond()
{
list($t1, $t2) = explode(' ', microtime());
return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
/**
* curl 方法
* @param String $url
* @param Array $data
* @return $tmpInfo
*/
public function curl($url, $data = null)
{
$ch = curl_init();
$header = array("Content-Type:application/x-www-form-urlencoded");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
}