微信公衆平臺小接口

    前兩天受人之託寫一個小接口,還被要求要使用特定的類和方法,因此就娛樂性的寫了這個小接口。最後實現大部分功能,特別是聊天小機器人後,發現挺好玩的。並且看到網上沒有多少關於wechat.class.php裏類和方法的講解,因此分享下這個當是小實例吧!事不宜遲,咱們開始吧。php

    首先接口配置和調試你們能夠到網上搜,那裏有比較詳細的圖文解說,這裏就不贅述了;其次是到https://github.com/dodgepudding/wechat-php-sdk下載wechat.class.php和snoopy.class.php,或者本身百度谷歌都會有的。好了以後就把下面的代碼放上去就好了,示例比較簡單,因此註釋就能夠說明咯。git

    若是發現什麼問題,歡迎討論,謝謝你們。祝學習進步。
github

<?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裏面相應的位置按下圖標記的地方修改。json

相關文章
相關標籤/搜索