public function get_access_token(){ $json=$this->http_request_json("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***"); $data=json_decode($json,true);
require ('menu.php');$wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); $wmenu=new wechatMenu(); $wwechatObj->post($wmenu->get_access_token(), $data);//$wechatObj->valid();class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){
2.再发送的自定义菜单的时候,看返回的数据是什么?根据错误代码去找出是哪里出错了。很显然,我不知道你这个方法是如何取得token值的?$token_url哪里来的? 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错误";
}
}
现在的问题是,我在微信的官网上调试,提示是成功的,但我客户端看不到
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;
//var_dump($result);
return $result;
}这是我现在创建菜单的方法,我打印看了下,token是正确获取到了,但菜单没创建成功,系统的打印是
{"errcode":-1,"errmsg":"system error"}
这个errorcode在微信官方看到的解释是系统繁忙,但我想应该不是这个问题,因为现在有人用java来创建是OK的,我想还是这块PHP写的有问题,谁帮忙看看
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;
}
第一个参数是你要输入的内容,
第二个参数不要改,
第三个参数是日志的名称,你可以加一下路径 如"c:\log.txt"你就可以在C盘看到这个文件了
/**
* wechat php test
*///define your token
define("TOKEN", "weixin");$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);
//$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=***&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;
}
?>这就是现在代码了,除了真实的appid和secret被我用***代替了
/**
* wechat php test
*/
//define your token$token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***";
class wechatMenu
{
public function get_access_token(){
$json=$this->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;
}
}
?>
将以上代写成menu.php然后调用
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "830961");$data = '{
"button":[
{
"name":"蛋糕目录",
"sub_button":[
{
"type":"click",
"name":"水果系列",
"key":"V1001_shuiguo"
},
{
"type":"click",
"name":"巧克力系列",
"key":"V1001_qiaokeli"
},
{
"type":"click",
"name":"鲜奶油蛋糕",
"key":"V1001_xiannai"
},
{
"type":"click",
"name":"冰淇淋蛋糕",
"key":"V1001_bingqilin"
}]
},
{
"type":"click",
"name":"歌手简介",
"key":"geshou"
},
{
"name":"菜单",
"sub_button":[
{
"type":"click",
"name":"hello word",
"key":"V1001_HELLO_word"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_good"
}]
}]
}';
require ('menu.php');$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
$wmenu=new wechatMenu();
$wwechatObj->post($wmenu->get_access_token(), $data);//$wechatObj->valid();class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"]; //valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
} public function responseMsg()
{ //get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);//$postObj是simpleXml的对象
$type = trim($postObj->MsgType);
$resultStr = $this->handleText($postObj);
echo $resultStr;
}else {
echo "2";
exit;
}
}
这个为什么没有我写的菜单的效果 ??