|
代码示例
[code]<?php
/**
* 天气查询模块处理程序
*
* @author sun
* @url http://bbs.ylsunyuan.com
*/
defined('IN_IA') or exit('Access Denied');
class Sun_weatherModuleProcessor extends WeModuleProcessor {
public function respond() {
$content = $this->message['content'];
//这里定义此模块进行消息处理时的具体过程, 请查看微擎文档来编写你的代码
preg_match("/^天气 (.*)/", $content, $res);
$res = $this -> pinyin($res[1],utf8);
$url = "http://apistore.baidu.com/microservice/weather?citypinyin=".$res;
$json = $this -> curlGet($url);
$res = json_decode($json,true);
if(count($res['retData'])>0){
$content = $res['retData']['city']." ".$res['retData']['weather'].",气温:".$res['retData']['temp']."度,风向:".$res['retData']['WD'].",风力:".$res['retData']['WS']."。";
}else{
$content = "抱歉,无法识别您输入的城市名称!";
}
return $this->respText($content);
}
public function curlGet($url)
{
//$url = "http://apistore.baidu.com/microservice/weather?citypinyin=beijing";
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$result = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//打印获得的数据
return $result;
}
public function pinyin($_String, $_Code='UTF8')
{ //GBK页面可改为gb2312,其他随意填写为UTF8
$_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha".
"|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|".
|
|