微信 php 自定义菜单
我申请了一个微信公众平台的测试账号,之前已经通过验证,关注后用我的微信号向测试账号发消息可以得到测试账号的正确响应,我现在想实现微信的自定义菜单,下面用***代替了我测试账号的appid和secret,我进测试账号没看到自定义菜单,请问问题出在哪?
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***";
$data = "{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"type":"click",
"name":"歌手简介",
"key":"V1001_TODAY_SINGER"
},
{
"name":"菜单",
"sub_button":[
{
"type":"click",
"name":"hello word",
"key":"V1001_HELLO_WORLD"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}";
$wechatObj = new wechatCallbackapiTest();
$wechatObj->post($wechatObj->get_access_token(), $data);
class wechatCallbackapiTest
{
public function get_access_token(){
$json=http_request_json($token_url);
$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($access_token, $jsonData){
$ch = curl_init("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token) ;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch) ;
curl_close($ch) ;
return $result;
}
}
?>
[解决办法]
我记得菜单在官方的文档有例子的
至于调试,应该官方会抛出错误编号方便你判断的吧.
[解决办法]
还有一个问题,我该怎么调试这种微信的php开发,在服务器上哪个地方可以看到日志(就是类似echo的值)?我是用xampp搭建的服务器
$wechatObj->get_access_token()
public function get_access_token(){
$json=http_request_json($token_url);
$data=json_decode($json,true);
if($data['access_token']){
return $data['access_token'];
}else{
return "获取access_token错误";
}
}
//创建菜单
function createMenu($data){
$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)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
还有一个问题,我该怎么调试这种微信的php开发,在服务器上哪个地方可以看到日志(就是类似echo的值)?我是用xampp搭建的服务器