现在的位置: 首页 > 微信开发 > 正文

微信开发模式,简单能运行的例子

2016年04月15日 微信开发 ⁄ 共 3128字 ⁄ 字号 评论关闭
 responseMsg();  //执行wechatCallbackapiTest类下的responseMsg()方法
}else{
wechatObj->valid(); //执行wechatCallbackapiTest类下的valid()方法
}

class wechatCallbackapiTest //创建wechatCallbackapiTest类
{
public function valid() //创建一个名为valid()的公共函数方法
{
echoStr = _GET["echostr"]; //收集get数据(随机字符串)

//valid signature , option
if(this->checkSignature()){ //判断checkSignature()自定义函数结果(验证微信服务器)
echo echoStr; //为真则输出结果(随机字符串)
exit;
}
}

public function responseMsg()
{
//get post data, May be due to the different environments
postStr = GLOBALS["HTTP_RAW_POST_DATA"];//获取post数据

//extract post data
if (!empty(postStr)){

postObj = simplexml_load_string(postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
fromUsername = postObj->FromUserName;
toUsername = postObj->ToUserName;
type = postObj->MsgType;
customrevent = postObj->Event;
latitude = postObj->Location_X;
longitude = postObj->Location_Y;
keyword = trim(postObj->Content);
time = time();
textTpl = "


%s


0
";
switch (type)
{ case "event";
if (customrevent=="subscribe")
{contentStr = "亲,你终于来啦~~感谢关注号!";}
break;
case "image";
contentStr = "你的图片很棒!";
break;
case "voice":
contentStr = "我不知道你在说什么!还是发送文字吧!\n";
break;
case "video";
contentStr = "你的视频很棒!";
break;
case "location";

data = file_get_contents("http://api.map.baidu.com/geoconv/v1/?coords={latitude},{longitude}&output=xml&from=3&to=5&ak=你的ak");
array = simplexml_load_string(data);
lat=array->result->point[0]->x;
log=array->result->point[0]->y;
// 腾讯地图坐标转换为百度地图坐标
textTpl = "


%s

4












0
";
resultStr = sprintf(textTpl, fromUsername, toUsername, time, url);
echo resultStr;
break;
case "link" ;
contentStr = "你的链接有病毒吧!";
break;
default;
contentStr ="此项功能尚未开发";
}

if(keyword=="天气"){

// 天气api接口 http://developer.baidu.com/map/carapi-7.htm
weatherurl="http://api.map.baidu.com/telematics/v2/weather?location=武汉&ak=你的ak";
apistr=file_get_contents(weatherurl);
apiobj=simplexml_load_string(apistr);
contentStr ="【武汉天气预报】\n{apiobj->results->result[0]->date}\n天气 {apiobj->results->result[0]->weather}\n风力 {apiobj->results->result[0]->wind}\n温度 {apiobj->results->result[0]->temperature}
\n明天{apiobj->results->result[1]->date}\n{apiobj->results->result[1]->weather} {apiobj->results->result[1]->wind} {apiobj->results->result[1]->temperature}
\n后天{apiobj->results->result[2]->date}\n{apiobj->results->result[2]->weather} {apiobj->results->result[2]->wind} {apiobj->results->result[2]->temperature}
\n{apiobj->results->result[3]->date}\n{apiobj->results->result[3]->weather} {apiobj->results->result[3]->wind} {apiobj->results->result[3]->temperature}";
}

if(keyword=="联系方式"){
msgType="text";
contentStr = "地址:";
resultStr = sprintf(textTpl, fromUsername, toUsername, time, msgType, contentStr);
echo resultStr;
}

if(keyword=="图文消息"){

textTpl = "


%s

3








0
";
          // 以上标红的地方都需要自己修改

resultStr = sprintf(textTpl, fromUsername, toUsername, time,url);
echo resultStr;
}
if(keyword=="历史消息"){

url = "历史消息网址";
resultStr = sprintf(textTpl, fromUsername, toUsername, time,url);
echo resultStr;

}

msgType="text";
resultStr = sprintf(textTpl, fromUsername, toUsername, time, msgType, contentStr);
echo resultStr;

}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;
}
}
}

抱歉!评论已关闭.

×