根据关键字返回音乐的微信公众号接口源码,可以加一下我的微信号 lweixin-com 体验一下
源码如下:
-
- /**
- * wechat php test
- */
-
- //define your token
- //header("Content-Type: text/html;charset=utf-8");
- define("TOKEN", "weixin");
- $$$$wechatObj = new wechatCallbackapiTest();
- $$$$wechatObj->responseMsg();
-
-
- class wechatCallbackapiTest
- {
- public function valid()
- {
- $$$$echoStr = $$$$_GET["echostr"];
-
- //valid signature , option
- if($$$$this->checkSignature()){
- echo $$$$echoStr;
- exit;
- }
- }
- public function song($$$$name){
- $$$$content2 = file_get_contents("http://shopcgi.qqmusic.qq.com/fcgi-bin/shopsearch.fcg?value=".$$$$name);
- $$$$str2=substr($$$$content2,15);
- $$$$str2=substr($$$$str2,0,-2);
- $$$$tempstrs = explode(",",$$$$str2);
- $$$$endstr = array();
- $$$$i=0;
-
- foreach($$$$tempstrs as $$$$tempstr)
- {
- $$$$endstr[$$$$i] = str_replace(":",'":',$$$$tempstr);
- if($$$$i != 0)
- {
- $$$$endstr[$$$$i] = '"'.$$$$endstr[$$$$i];
- }
- $$$$i++;
- }
- $$$$endstr[0] = str_replace('{','{"',$$$$endstr[0]);
- $$$$endstr[5] = str_replace('[{','[{"',$$$$endstr[5]);
- $$$$str2 = implode(",",$$$$endstr);
- $$$$str2 = str_replace("[","",$$$$str2);
- $$$$str2 = str_replace("]","",$$$$str2);
- $$$$str2 = str_replace(""{","{"",$$$$str2);
- $$$$tempstrs = explode(",",$$$$str2);
- $$$$tempstrs[5] = '"songlist1":{"idx":"1"';
- $$$$i=0;
- $$$$x=2;
- foreach($$$$tempstrs as $$$$json){
- $$$$jsons[$$$$i] = iconv('gb2312', 'utf-8//IGNORE', $$$$json);
- if(substr($$$$json,0,8)=='{"idx":"'){
- $$$$a = $$$$i-1;
- $$$$jsons[$$$$i] = iconv('gb2312', 'utf-8//IGNORE', '"songlist'.$$$$x.'":{"idx":"'.$$$$x.'"');
- $$$$x++;
- }
- $$$$i++;
- }
- $$$$str = implode(",",$$$$jsons);
- $$$$result=json_decode($$$$str,true);
- return $$$$result;
- }
- public function responseMsg()
- {
- //get post data, May be due to the different environments
- @$$$$postStr = $$$$GLOBALS["HTTP_RAW_POST_DATA"];
-
- //extract post data
- if (!empty($$$$postStr)){
-
- $$$$postObj = simplexml_load_string($$$$postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $$$$fromUsername = $$$$postObj->FromUserName;
- $$$$toUsername = $$$$postObj->ToUserName;
- $$$$keyword = trim($$$$postObj->Content);
- $$$$time = time();
- $$$$textTpl = "
-
-
-
- %s
-
-
-
-
-
-
-
- 0
-
";
- if(!empty( $$$$keyword ))
- {
- $$$$msgType = "music";
- $$$$songname = iconv('utf-8','gb2312',$$$$keyword);
- $$$$songnameurlcode = urlencode($$$$songname);
- $$$$musicinfo = $$$$this->song($$$$songnameurlcode);
- $$$$musicurl = 'http://stream1'.$$$$musicinfo[songlist1][location].".qqmusic.qq.com/3".$$$$musicinfo[songlist1][song_id].".mp3";
- $$$$resultStr = sprintf($$$$textTpl, $$$$fromUsername, $$$$toUsername, $$$$time, $$$$msgType, $$$$musicinfo[songlist1][song_name],"歌手:".$$$$musicinfo[songlist1][singer_name]." 专辑:".$$$$musicinfo[songlist1][album_name],$$$$musicurl,$$$$musicurl);
- echo $$$$resultStr;
- }else{
- echo "Input something...";
- }
-
- }else {
- echo "";
- exit;
- }
- }
-
- private function checkSignature()
- {
- $$$$signature = $$$$_GET["signature"];
- $$$$timestamp = $$$$_GET["timestamp"];
- $$$$nonce = $$$$_GET["nonce"];
-
- $$$$token = TOKEN;
- $$$$tmpArr = array($$$$token, $$$$timestamp, $$$$nonce);
- sort($$$$tmpArr);
- $$$$tmpStr = implode( $$$$tmpArr );
- $$$$tmpStr = sha1( $$$$tmpStr );
-
- if( $$$$tmpStr == $$$$signature ){
- return true;
- }else{
- return false;
- }
- }
- }
-