使用虾米音乐API 搜索音乐

2013-03-07 浏览:1564
使用虾米音乐API 搜索音乐
评论:(0)复制地址

使用get方法访问相关页面 返回json数组

Php代码

<?php
/**
 * music search action
 * @name music_name
 * @page
 */

$name=get_input('name','aa');
$name = urlencode($name);
$page=max(1,intval(get_input('page',1)));
$url="http:/"."/www.xiami.com/app/nineteen/search/key/$name/page/$page/size/1?random=1123132112132";
$file_contents = file_get_contents($url);
echo $file_contents;
exit;

Js代码

/**
*根据虾米 搜索音乐
*/
elgg.thewire.musicSearch = music_search;
 function music_search(){
        var music_name=$('#music_name');
        var pages=$('#music_page').val();
        if(pages == null){
            pages = 1;
        }
        if(music_name.val()==''){
            alert('请输入需要搜索的歌名');
            music_name.focus();
            return false;
        }
        $('#music_list').html('<span style="color:red"><img src="<?php echo elgg_get_site_url()?>images/loading.gif">正在检索,请稍等...</span>');
        $.ajax({
            type:'post',
            url:elgg.security.addToken('<?php echo elgg_get_site_url()?>action/music/search'),
            data:{name:music_name.val(),page:pages},
            success:function(d){
                //alert(d);
                // return false;
                d= eval('('+d+')');
                var json=eval(d.results);
                if(json.length>0){
                    var totle = d.total;
                    var num = Math.ceil(totle/8);

                    var phtml = page_html(pages,num);
                    //var phtml='';
                    $('#music_list').html('<ul class="tagB" style="display:block"><div id="add_ajax_favorite_tags" class="music_end"></div><div>'+ phtml +'</div></ul>');

                    for(var i=0; i<json.length; i++)  {
                        json[i].song_name = decodeURIComponent(json[i].song_name).replace(/\+|'|"/g," ");
                        json[i].artist_name = decodeURIComponent(json[i].artist_name).replace(/\+|'|"/g," ");
                        var html='<span onclick="check_music('+json[i].song_id+',\''+decodeURI(json[i].artist_name)+'\',\''+decodeURI(json[i].song_name)+'\')" style="width:100%;cursor:pointer;" onmouseover="$(this).css(\'color\',\'red\');" onmouseout="$(this).css(\'color\',\'\');">'+decodeURI(json[i].song_name)+' ---  '+decodeURI(json[i].artist_name)+'</span>';
                        $('.music_end').append(html+'<br>');
                    }
                }else{
                    $('#music_list').html("未检索到符合条件的歌曲");
                }
            }
        });
};
/**
     * 制作虾米音乐的简单HTML分页代码 page:当前页 num:总页数
     */
    function page_html(page,num){
        page = parseInt(page);
        num = parseInt(num);

        var html = '';
        if(num < 2){
            return '';
        }
        if(page > 1){
            var fpage = page-1;
            html += '<span onclick="changepage('+ fpage +');" style="cursor:pointer;">上一页</span>';
        }
        if(page < num){
            var npage = page+1;
            html += '<span onclick="changepage('+ npage +');" style="cursor:pointer;">下一页</span>';
        }
        return html;
    }

    function changepage(page){
        $('#music_page').val(page);
        music_search();
    }

    //选择音乐
    function check_music(music_id,name,music_name){

        var content = music_name + '--' + name+' [music:'+music_id+']';
        //alert(content);
        var thewire=$('#thewire-textarea').val();
        if(thewire!=''){
            $('#thewire-textarea').val(thewire+content);
        }else{
            $('#thewire-textarea').val(content);
        }
        $("#music_show").hide();
    }

Php代码

/**
 * 解析 音乐
 * @param type $text
 * @return type
 */
function parsemusic($text){
 $text=trim($text);
 $music_pattern='/(\[music:)(\d+)(\])/';
 $musci_html='<div class="feedUserImg"><embed width="257" height="33"wmode="transparent" type="application/x-shockwave-flash"src="http://www.xiami.com/widget/0_${2}/singlePlayer.swf"></embed></div>';
 $text= preg_replace($music_pattern,$musci_html,$text);
 return $text;
}

评论:(0)复制地址
发布:苗景云 | 分类:IT技术&设计 | Tags:虾米 音乐 API

相关文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。