易信公众平台接口文件代码:能用在WC上吗?
易信公众平台接口文件代码,只要将文件里的token换成自己定义的就可以直接放到自己的空间上了。注意:token值要跟您在开发模式那里写的一样。
{{{
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "自己设定的值");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()//接受用户信息并返回图文信息
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml> ";
//加载图文模版
$picTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml> ";
if(trim($postObj->MsgType) == "event" and trim($postObj->Event) == "subscribe")//判断是否是新关注
{
$msgType = "news";
$title = "*****";
$data = date('Y-m-d');
$desription = "地址:*****";
$image = "*****";
$turl = "*****";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl);
echo $resultStr;
}elseif(!empty($keyword ))//用户输入的内容
{
$msgType = "news";
$title = "*****";
$data = date('Y-m-d');
$desription = "地址:*****";
$image = "*****";
$turl = "*****";
$resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl);
echo $resultStr;
}else{
echo "说点什么吧!";
}
}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;
}
}
}
?>
好了,只要点击易信公众平台上的提交验证,验证成功就说明已经OK了!
接着,我再跟大家说说如何做自定义菜单功能,其实这也不是很难,下面我就给上代码 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<?php
$xjson = '{
"button":[
{
"type":"view",
"name":"*****",
"url":"*****"
},
{
"name":"*****",
"sub_button":[
{
"type":"view",
"name":"*****",
"url":"*****"
},
{
"type":"view",
"name":"*****",
"url":"*****"
},
{
"type":"view",
"name":"*****",
"url":"*****"
}]
},
{
"name":"*****",
"sub_button":[
{
"type":"click",
"name":"*****",
"key":"V1001_HELLO_WORLD"
},
{
"type":"view",
"name":"*****",
"url":"*****"
},
{
"type":"view",
"name":"*****",
"url":"*****"
}]
}]
}';
//$wxurl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=您的token码"; //微信POST数据地址,换为您的token码即可。
$yxurl = "https://api.yixin.im/cgi-bin/menu/create?access_token=您的token码"; //易信POST数据地址
$result = vpost($yxurl,$xjson); //POST数据并返回结果,如果是微信,请将$yxurl改为$wxurl
var_dump($result); //输出结果返回码
function vpost($url,$data){ // 模拟POST提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
// curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
// curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据
}
?>
</body>
</html>
}}}
【 文章来源于:玖壹视野 胡汉明博客】
2014-01-18 03:37