测试页面menu.php展示:
~~~
<?php
function http_curl($url,$data=null){//封装curl
// 1. 初始化
$ch = curl_init();
// 2. 设置选项,包括URL
curl_setopt($ch,CURLOPT_URL,$url);
//curl_setopt($ch,CURLOPT_HEADER,0);
//下面两个是php5.6版本需加上才能实现
curl_setopt($ch,CURLOPT_SAFE_UPLOAD, FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
if(!empty($data)){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//测试号写上这个是跳过SSL证书检查,返回结果才不会null;
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//禁止curl资源直接输出
// 3.抓取url并把它传递给服务器
$opt=curl_exec($ch);
// 4. 释放curl句柄
curl_close($ch);
return $opt;
}
function get_token(){//获取access_token
$appid="wx165112bf167af76c";
$secret="c65b22bfcf03fdd98504eac299701b03";
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$json=http_curl($url);
$result=json_decode($json);
return $result->access_token;
}
$token=get_token();
$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token}";
$data=<<<php
{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"view",
"name":"视频",
"url":"http://v.qq.com/"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}
php;
$result=http_curl($url,$data);
var_dump($result);
/*
//var_dump($token);
$type="image";
//组装图片在Windows服务器上,格式示例为:”@F:\israel\upload\winter.jpg”,而在Linux服务器上,格式示例为:”@home/israel/upload/winter.jpg”。
//$path="C:\ftp\wx\xiaotu.jpg";
$path=dirname(__FILE__)."/xiaotu.jpg";
//组装路径
$data=array("media"=>"@".$path);
$url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
$arr=http_curl($url,$data);
var_dump($arr);*/
?>
~~~
相关知识点代码:
![](https://box.kancloud.cn/9537b486968f42df36d5b8794deb649b_1136x640.png)
浏览器结果展示如下图则生成成功
![](https://box.kancloud.cn/6dacdd711adf109b29fedd32ea1e3405_487x177.png)
打开公众号检验:
![](https://box.kancloud.cn/2bf45cd33f3bc87ae3daebc7d3152a23_425x342.png)