<?php
define("TOKEN", "xujing");
$wechatObj = new wechatCallbackapiTest(); //创建对象;
if (!isset($_GET['echostr'])) { //iset用于检测变量是否赋值。
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest //创建类
{
//验证签名
public function valid()
{
$echoStr = $_GET["echostr"]; //获取随机字符串
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"]; // 获取时间戳
$nonce = $_GET["nonce"]; //随机数值
$token = TOKEN
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr); //排序
$tmpStr = implode($tmpArr); //把数组元素组合成字符串
$tmpStr = sha1($tmpStr); //sha1() 函数计算字符串的 SHA-1 散列,成功返回序列,否则返回false
if($tmpStr == $signature){
echo $echoStr; //如果验证身份成功,则返回值。
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];// 接收data格式。
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);// 把 XML 字符串载入对象中。如果失败,则返回 false。把$postStr转成简单的'SimpleXMLElement'。
$RX_TYPE = trim($postObj->MsgType);//trim() 函数从字符串的两端删除空白字符和其他预定义字符。
$result = "";
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注后会有柒,回复任意城市名,如:北京。即可获得该城市的近期天气预报";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
private function receiveText($object)
{
$keyword = trim($object->Content);
$url = "http://apix.sinaapp.com/weather/?appkey=".$object->ToUserName."&city=".urlencode($keyword); //此处是在调用接口。该接口不用自己找到源码编辑,直接用即可。
$output = file_get_contents($url);//把整个文件读入一个字符串中于.将文件的内容读入到一个字符串中的首选方法
$content = json_decode($output, true);
//接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$result = $this->transmitNews($object, $content);
return $result;
}
private function transmitText($object, $content)
{
if (!isset($content) || empty($content)){
return "";
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); //把数据插入textTP1中的%处。
return $result;
}
private function transmitNews($object, $newsArray)
{
if(!is_array($newsArray)){
return "";
}
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($newsArray as $item){
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); // .= 代表字符串拼接
}
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}
private function logger($log_content)
{
}
}
?>
define("TOKEN", "xujing");
$wechatObj = new wechatCallbackapiTest(); //创建对象;
if (!isset($_GET['echostr'])) { //iset用于检测变量是否赋值。
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest //创建类
{
//验证签名
public function valid()
{
$echoStr = $_GET["echostr"]; //获取随机字符串
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"]; // 获取时间戳
$nonce = $_GET["nonce"]; //随机数值
$token = TOKEN
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr); //排序
$tmpStr = implode($tmpArr); //把数组元素组合成字符串
$tmpStr = sha1($tmpStr); //sha1() 函数计算字符串的 SHA-1 散列,成功返回序列,否则返回false
if($tmpStr == $signature){
echo $echoStr; //如果验证身份成功,则返回值。
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];// 接收data格式。
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);// 把 XML 字符串载入对象中。如果失败,则返回 false。把$postStr转成简单的'SimpleXMLElement'。
$RX_TYPE = trim($postObj->MsgType);//trim() 函数从字符串的两端删除空白字符和其他预定义字符。
$result = "";
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注后会有柒,回复任意城市名,如:北京。即可获得该城市的近期天气预报";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
private function receiveText($object)
{
$keyword = trim($object->Content);
$url = "http://apix.sinaapp.com/weather/?appkey=".$object->ToUserName."&city=".urlencode($keyword); //此处是在调用接口。该接口不用自己找到源码编辑,直接用即可。
$output = file_get_contents($url);//把整个文件读入一个字符串中于.将文件的内容读入到一个字符串中的首选方法
$content = json_decode($output, true);
//接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$result = $this->transmitNews($object, $content);
return $result;
}
private function transmitText($object, $content)
{
if (!isset($content) || empty($content)){
return "";
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); //把数据插入textTP1中的%处。
return $result;
}
private function transmitNews($object, $newsArray)
{
if(!is_array($newsArray)){
return "";
}
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($newsArray as $item){
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); // .= 代表字符串拼接
}
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}
private function logger($log_content)
{
}
}
?>