想开发微信自定义菜单,菜单命名为menu.php
在wx_sample.php中调用menu.php设计的菜单
menu.php的代码如下(测试过,应该没问题)
<?php
header("Content-type: text/html; charset=utf-8");
header("Content-type: text/html; charset=gb2312");
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***";
$content = file_get_contents($url);
$info = json_decode($content);
print_r($info);
echo $info->access_token;
$ACCESS_TOKEN=$info->access_token;
$data = '{
"button":[
{
"type":"click",
"name":"Find",
"key":"find"
},
{
"type":"click",
"name":"Check",
"key":"lock_acount"
},
{
"type":"click",
"name":"About",
"key":"other"
}]
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
var_dump($tmpInfo);
?>
怎么样实现wx_sample.php中调用这个菜单呢?代码是啥?现在的代码在下面求大神相助!!!!
<?php
/**
* wechat php test
*///define your token
define("TOKEN", "sunweilin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"]; //valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
在wx_sample.php中调用menu.php设计的菜单
menu.php的代码如下(测试过,应该没问题)
<?php
header("Content-type: text/html; charset=utf-8");
header("Content-type: text/html; charset=gb2312");
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***";
$content = file_get_contents($url);
$info = json_decode($content);
print_r($info);
echo $info->access_token;
$ACCESS_TOKEN=$info->access_token;
$data = '{
"button":[
{
"type":"click",
"name":"Find",
"key":"find"
},
{
"type":"click",
"name":"Check",
"key":"lock_acount"
},
{
"type":"click",
"name":"About",
"key":"other"
}]
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
var_dump($tmpInfo);
?>
怎么样实现wx_sample.php中调用这个菜单呢?代码是啥?现在的代码在下面求大神相助!!!!
<?php
/**
* wechat php test
*///define your token
define("TOKEN", "sunweilin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"]; //valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
/**
* wechat php test
*/
//define your token
define("TOKEN", "sunweilin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
$xml = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
$this->request = array_change_key_case($xml, CASE_LOWER);
if($this->request ['msgtype'] == 'event'){
if($this->request['event'] == 'click'){
if( $this->request['eventkey'] == 'lock_acount'){
//方法
}
}
}
}private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>