<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "abcabcabc");
$data='{
"button": [
{
"name": "公共查询",
"sub_button": [
{
"type": "click",
"name": "查询1",
"key": "1"
},
{
"type": "click",
"name": "查询2",
"key": "2",
"sub_button": [ ]
},
{
"type": "click",
"name": "查询3",
"key": "3"
}
]
},
{
"name": "客户查询",
"sub_button": [
{
"type": "click",
"name": "查询4",
"key": "4"
},
{
"type": "click",
"name": "查询5",
"key": "5"
},
{
"type": "click",
"name": "查询6",
"key": "6"
}
]
},
{
"name": "关于我们",
"sub_button": [
{
"type": "click",
"name": "查询7",
"key": "MicroWebSite"
},
{
"type": "click",
"name": "员工通道",
"key": "EmployeePortal"
}
]
}
]
}';
$wechatObj = new wechatCallbackapiTest();
$wechatObj->post($wechatObj->get_access_token(), $data);
//$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function get_access_token(){
$json=$this->http_request_json("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=自己的APPID&secret=自己的secret");
$data=json_decode($json,true);
if($data['access_token']){
return $data['access_token'];
}else{
return "获取access_token错误";
}
}
public function http_request_json($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function post($token, $jsonData){
echo $token;
$MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token;
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL, $MENU_URL);
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, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch) ;
if (curl_errno($ch)) {
echo 'Error'.curl_error($ch);
}
curl_close($ch) ;
echo $result;
return $result;
}
}
?>