微信公众平台小接口

    前两天受人之托写一个小接口,还被要求要使用特定的类和方法,所以就娱乐性的写了这个小接口。最后实现大部分功能,特别是聊天小机器人后,发现挺好玩的。而且看到网上没有多少关于wechat.class.php里类和方法的讲解,所以分享下这个当是小实例吧!事不宜迟,我们开始吧。

    首先接口配置和调试大家可以到网上搜,那里有比较详细的图文解说,这里就不赘述了;其次是到https://github.com/dodgepudding/wechat-php-sdk下载wechat.class.php和snoopy.class.php,或者自己百度谷歌都会有的。好了之后就把下面的代码放上去就行了,示例比较简单,所以注释就可以说明咯。

    如果发现什么问题,欢迎讨论,谢谢大家。祝学习进步。

<?php
//导入微信函数库
include "wechat.class.php";
//导入采集函数库
include "snoopy.class.php";
//填写你设定的key
$options = array(
		'token'=>'yrw' 
	);
$weObj = new Wechat($options);
$weObj->valid();
//获得用户发送过来的消息的类型,有"text","music","image"等
$type = $weObj->getRev()->getRevType();
switch($type) {
	case Wechat::MSGTYPE_TEXT:
			//获得用户发送过来的文字消息内容
			$content=$weObj->getRev()->getRevContent();
			//从消息的结尾数第二个字开始截取,截取两个字
			$str = mb_substr($content,-2,2,"UTF-8");
			//从消息的开头开始,截掉末尾的两个字,便得关键字。
			$str_key = mb_substr($content,0,-2,"UTF-8");
			//然后加以判断是否为关键字,是否为空,符合要求则调用函数查询翻译数据
			if($str == '翻译' && !empty($str_key)) {
			    $url1="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=9peNkh97N6B9GGj9zBke9tGQ&q={$str_key}&from=auto&to=auto";//百度翻译地址
				//实例化采集类
				$spy=new Snoopy;
				//获取采集来的数据
				$spy->fetch($url1);
				//将结果赋给$con_json
				$con_json=$spy->results;
				//json解析,转换为object对象类型
				$transon=json_decode($con_json);
				//读取翻译内容
				$con_str = $transon->trans_result[0]->dst;
				//以文字的形式输出结果
				$weObj->text("{$con_str}")->reply();
			}
			//判断是否为关键字,是否为空,符合要求则调用函数查询书本数据
			if($str=='书本' && !empty($str_key)) {				           		
				$url="http://222.206.65.12/opac/search_rss.php?dept=ALL&title={$str_key}&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=20&showmode=list&orderby=DESC&sort=CATA_DATE&onlylendable=no";
				$spp=new Snoopy;
				$spp->fetch($url);
				$fa=$spp->results;
				//将采集获取的XML数据转换成object对象类型
				$f=simplexml_load_string($fa);
				$da1=$f->channel->item[0]->title;
				$da2=$f->channel->item[1]->title;
             	$da3=$f->channel->item[2]->title;		
             	$weObj->text("{$da1}\n{$da2}\n{$da3}")->reply();
			}
			//判断是否为关键字,是否为空,符合要求则调用函数查询天气数据
			if($str == '天气' && !empty($str_key)) {
				$url="http://api.map.baidu.com/telematics/v2/weather?location={$str_key}&ak=1a3cde429f38434f1811a75e1a90310c";
				$sp=new Snoopy;
				$sp->fetch($url);
				$l_xml=$sp->results;
				$f=simplexml_load_string($l_xml);
				$city=$f->currentCity;
				$da1=$f->results->result[0]->date;
				$da2=$f->results->result[1]->date;
             	$da3=$f->results->result[2]->date;		
				$w1=$f->results->result[0]->weather;
				$w2=$f->results->result[1]->weather;
             	$w3=$f->results->result[2]->weather;		
				$p1=$f->results->result[0]->wind;
				$p2=$f->results->result[1]->wind;
             	$p3=$f->results->result[2]->wind;
				$q1=$f->results->result[0]->temperature;
				$q2=$f->results->result[1]->temperature;
             	$q3=$f->results->result[2]->temperature;
             	$k1=$f->results->result[0]->dayPictureUrl; 
         		$k2=$f->results->result[1]->dayPictureUrl;
         		$k3=$f->results->result[2]->dayPictureUrl; 	
				$d1=$city.$da1.$w1.$p1.$q1;
				$d2=$city.$da2.$w2.$p2.$q2;
				$d3=$city.$da3.$w3.$p3.$q3;
				$weObj->text("{$d1}\n{$d2}\n{$d3}")->reply();
			}
			if($content=="hi") {
			$weObj->text("hello,I'm pauky")->reply();
			}else if($content=="超链接") {
				$weObj->text("http://m.baidu.com")->reply();
			}else if($content=="音乐") {
				//推送音乐消息
				$weObj->music("music","pauky","http://jiekouphp.duapp.com/hls.mp3","http://jiekouphp.duapp.com/hls.mp3")->reply();
			}
			/*
			剩下的任务就交给我们的小黄鸭机器人;
			下面这个是临时密钥,只有几天使用期限,大家可以在http://developer.simsimi.com/申请,然后修改下面url的key值就可以了。
			*/
			else {
                $strurl="http://sandbox.api.simsimi.com/request.p?key=7bff80d9-cff9-489e-92fb-167b31392e91&lc=ch&ft=1.0&text={$content}";
                $xhy=new Snoopy;
				$xhy->fetch($strurl);
				$x_json=$xhy->results;
				$strjson=json_decode($x_json);
				$contentStr = $strjson->{'response'};
                $weObj->text("{$contentStr}")->reply();
			}
			break;
	case Wechat::MSGTYPE_LOCATION:
				//接收消息的地理位置
				$arr1=$weObj->getRev()->getRevGeo();
				$snoopy=new Snoopy;
                $url="http://api.map.baidu.com/telematics/v2/distance?waypoints=116.68183799999997,23.353299;{$arr1['x']},{$arr1['y']}&ak=1a3cde429f38434f1811a75e1a90310c";
				$snoopy->fetch($url);
				$lines_string=$snoopy->results; 
				$fk=simplexml_load_string($lines_string);
				$juli=$fk->results->distance;
				$contentstring="你和我的距离有{$juli}米远";
				$weObj->text("{$contentstring}")->reply();
			break;
	//接收到图片消息后随机回复一句话
	case Wechat::MSGTYPE_IMAGE:
				$b=" ";
				$a=rand(1,3);
				 switch ($a)
				 {case 1;
				 $b="健康好人缘,婚姻幸福美满";
				 break;
				 case 2;
				 $b="贵人相助化险为夷的好运纹";
				 break;
				 default;
				 $b="才华横溢好研究,知识丰富事业有成";	
				 }				 								 							
                $weObj->text("哈哈我知道这是图片\n祝你:{$b}")->reply();
                //exit;	
			break;
	//实现首次关注回复功能
	case Wechat::MSGTYPE_EVENT:
			$msgEvent=$weObj->getRev()->getRevEvent();
			$weObj->text("感谢您关注回禄持苇\n查天气:城市+天气,如广州天气\n翻译:字词+翻译,如好翻译\n测距:发送位置\n查书:书名+书本,如php书本\n听歌:回复音乐\n还可选择发送图片,小黄鸭机器人也会来和大家聊天哦!\n更多内容,敬请期待...")->reply();
			break;
	default:
			$weObj->text("查天气:城市+天气,如广州天气\n翻译:字词+翻译,如好翻译\n测距:发送位置\n查书:书名+书本,如php书本\n听歌:回复音乐\n还可选择发送图片,谢谢你的关注,更多内容,敬请期待...")->reply();

}

注:微信公众平台SDK更新,所以在上述wechat.class.php里面相应的位置按下图标记的地方修改。

191342_Xtoj_1446867.jpg




转载于:https://my.oschina.net/u/1446867/blog/205804

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值