微信小程序入门教程(一)API接口数据记录

时间:2023-03-09 00:30:08
微信小程序入门教程(一)API接口数据记录

今天测试用小程序调用API接口,发现有些数据打印都是对象,怎么全部打印详细点来

小程序代码:

  httpsearch: function (name, offset, type, cb) {
wx.request({
url: 'https://www.tinywan.com/api/wechat/songsSearch',
data: {
name: name,
offset: offset,
limit: 20,
type: type
},
method: 'GET',
success: function (res) {var arr = res.data.playlist.item;
for (var x in arr){
console.log(x);
console.log(arr[x]);
}
}
})
}

服务端代码:

    public function songsSearch()
{
$name = input('param.name');
$offset = input('param.offset');
$limit = input('param.limit');
$type = input('param.type');
$curlReq = curl_request('http://stream.aliyun.com/api/search/get/',[
's'=>$name,
'limit'=>$limit,
'type'=>$type,
'offset'=>$offset,
]);
$arrRes = json_decode($curlReq,true);
$newArr = [];
foreach ($arrRes['result']['songs'] as $key=>$val){
$newArr[] = [
'id'=>$val['id'],
'name'=>$val['name'],
'creatorId'=>$val['artists'][0]['id'],
'creator'=>$val['artists'][0]['name'],
'img1v1Url'=>$val['artists'][0]['img1v1Url'],
];
}
$res = [
'code' => 200,
'msg' => 'success',
'playlist' =>[
'item'=> $newArr,
'songCount'=> $arrRes['result']['songCount'],
]
];
return json($res);
}

打印结果

微信小程序入门教程(一)API接口数据记录