[微信查询接口] 微信三、歌曲点播

[复制链接]
发表于 2014-2-2 13:47:27 | 显示全部楼层 |阅读模式
歌曲点播
/ d& _5 T  k9 L6 f  Z4 w8 J: n; i' N$ _微信点歌其实已有很多博客写过,不过重复超超轮子就当锻炼锻炼身体嘛。先上效果图2 ?0 ^5 W) l9 h  f# ]! T  l' ~

, L# M6 d/ u+ O# ?
- b  Z8 R- g- Z
/ P0 a+ c4 J3 v% {0 S& U; p. \+ W回复2.2@谢谢你的爱,它先会去找这首歌,然后返回回来,如果找的不是你想点的歌曲,则需要中上歌手名称,如2.2@谢谢你的爱谢霆锋8 e8 r3 A9 y8 \) @/ d  ^' y
3 s. w, l5 h7 n$ f( K
2 i( l, j" e5 ~) X

# a: C( t+ L: E3 ^) V# M* K, q代码部分如下& R) Z$ c* T9 X& d1 s9 n8 D3 p
) l0 B# B* D2 I+ N4 s2 r
1、统一回复调用方法
  • //这里只对文本消息进行处理
  • private function reply($data){
  •     if('text' == $data['MsgType']){
  •         $text=$data["Content"];
  •         if($text=="?"){
  •             return $this->get_main_datas();
  •         }
  •         $_menu_text=strpos($text,"@")===false?$textstring)doubleval($text);//当需要查询时,指令为1.1@xxx使用进行doubleval转换
  •         if(array_key_exists($_menu_text, $this->all_info)){//存在于菜单中,当输入,1 2 3 时
  •             return $this->get_replay_datas($_menu_text,$text,$data["FromUserName"]);
  •         }
  •         return $this->hello();
  •     } elseif('event' == $data['MsgType' && 'subscribe' == $data['Event']){
  •         //订阅写入数据表
  •         return $this->hello();
  •         //$reply = array('欢迎您关注桂林小魔方童装公众助手! 打开我们的网站:http://aidemofang.com 可以了解最新的款式和价格.', 'text');
  •     } else {
  •         exit;
  •     }
  • }
  •      
  •     //取得回复的信息
  •     private function get_replay_datas($_menu_text,$text,$fromUser){
  •         $menu_text=strlen($_menu_text)==1?$_menu_text-1:$_menu_text;
  •         $method = $this->get_method($menu_text);
  •         $param=null;
  •         if(strpos($text,"@")!==false){
  •             $arr = preg_split("/@/", $text);
  •             $param=$arr[1];
  •         }
  •         return $this->$method($param,$fromUser);
  •     }
    5 r- H) F) U( t/ Y' j( O- o0 x0 M
复制代码
% F' I& r( A  R: G& L# W! e

+ ?: a+ q: |0 j2 d8 ?2、点歌实现
  • //歌曲点播
  •     public function get_song($song_title,$fromUser){
  •         //$song_title="春天里";
  •   
  •         $m_param=array(
  •             "op"    =>"7",
  •             "mode"  =>"1",
  •             "count" =>"1",
  •             "title" =>$song_title
  •         );
  •         $song_datas=http("http://box.zhangmen.baidu.com/x",$m_param);  
  •         $song_utf8=utf8($song_datas,"gbk");
  •   
  •         preg_match_all('/\<name\>(.*?)\<\/name\>/s', $song_utf8, $matchs);
  •         if(count($matchs)>1){
  •             $song_name=$matchs[1][0];
  •         }
  •         if(empty($song_name)){
  •             return array("未找到这首歌,检查一下网络或咱们换一首吧.","text");
  •         }
  •   
  •         $param=array(
  •             "op"    =>"12",
  •             "count" =>"1",
  •             "title" =>trim("$song_name")
  •         );
  •         //查找作者的这首歌
  •         $datas=http("http://box.zhangmen.baidu.com/x",$param);
  •   
  •         $xml = new DOMDocument();
  •         $xml->loadXML($datas);
  •   
  •         $lst=$xml->getElementsByTagName('encode');
  •   
  •         //普通质量地址
  •         $item=$lst->item(0);
  •         $pre=$item->nodeValue;
  •         $suffix=$item->nextSibling->nodeValue;
  •         $MusicURL=str_replace(strrchr($pre, "/"),"",$pre)."/".str_replace(strrchr($suffix, "&"),"",$suffix);
  •   
  •   
  •         //高质量地址
  •         $item=$lst->item(0);
  •         $pre=$item->nodeValue;
  •         $suffix=$item->nextSibling->nodeValue;
  •         $HQMusicUrl=str_replace(strrchr($pre, "/"),"",$pre)."/".str_replace(strrchr($suffix, "&"),"",$suffix);
  •   
  •   
  •         $datas=array();
  •         $datas[]=str_replace('$', "", $song_name);//音乐标题
  •         $datas[]="来自互联网";//音乐描述
  •         $datas[]="$MusicURL";//音乐链接
  •         $datas[]="$HQMusicUrl";//高质量音乐链接,WIFI环境优先使用该链接播放音乐
  •   
  •         $this->sendLyric(str_replace('$$', "-", $song_name),$fromUser);
  •         return array($datas,"music");
  •   
  •     }
  •   
  •     private function sendLyric($song_name,$fromUser){
  •         $param=array(
  •             "key"   => "free",
  •             "appid" =>   "0",
  •             "msg"   =>   "歌词".$song_name
  •         );
  •         $datas=http("http://api.ajaxsns.com/api.php",$param);
  •         $json=json_decode($datas);
  •         if($json->result==0){
  •             $content=str_replace("{br}","\n",$json->content);
  •             $content.="\n\n小助手:回复 @ 进入主界面";
  •             import('@.ORG.ThinkWechat');
  •             $weixin = new ThinkWechat ();
  •             $result=$weixin->sendMsg($content, $fromUser, "text" );//发送歌词
  •   
  •             Log::write(" init data ->".$fromUser." ,content =$content");
  •             Log::write("result -> ".print_r($result,true));
  •         }
  •          
  •     }</name\>
    : X  L8 @& V! L% _
复制代码

8 \* {# c3 L+ K4 y% o3 `3 i1 _7 j5 h' W8 w2 a& A' [

; A1 G  V% l+ G/ O- {. w1 S+ r8 M8 H7 R* B! V/ `
*上面增加了发送歌词接口,自动发送给客户端,可惜我是订阅号,似乎没有那上权限,so,放弃吧.# d  \  S8 W8 u. S: W8 |1 X5 X




上一篇:微信二、获取笑话
下一篇:四、中英翻译、歌词、藏头诗、智能聊天
回复

使用道具 举报

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

快速回复 返回顶部 返回列表