1. 程式人生 > >建立自定義選單

建立自定義選單

<?php 
header("Content-type:text/html;charset=utf-8");
/**
 * 建立自定義選單
 */
$menuData = '{
	"button":[
		{
			"type":"click",
			"name":"紫月",
			"key" :"01"
		},
		{
			"name":"分類操作",
			"sub_button":[
				{
					"type":"click",
					"name":"圖片[click]",
					"key" :"aaa"
				},
				{
					"type":"view",
					"name":"百度一下[view]",
					"url" :"http://www.baidu.com"
				},
				{
					"type":"view",
					"name":"美圖",
					"url" :"http://p2.gexing.com/shaitu/20121020/1231/5082292c2b10c.jpg"
				}
			]
		},
		{
			"type":"click",
			"name":"美圖",
			"key" :"bbb"
		},
		
	]
}';

// 設定token
// [說明]此處token是用(微信公眾平臺介面除錯工具)提前生成
$token = "dBBvPXeNq03PXwPE3YIRlZbkD7d23TIWw2YM8dgAxrgZpknwbfmjMhYT526QZAcGinbTDHBMNg83M7qEf-AWif11gVHM3idJkId1w0p8I8cXGBaAEALUP";
// 設定URL
// [說明]https需安全認證,curl需設定引數跳過認證才能訪問 或 修改https為http
$url = "http://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token;

// curl POST訪問
$ch = curl_init();
// [注意]當訪問模式為http時,以下兩個引數的設定可省略
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 設為false跳過證書檢查
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 從證書中檢查SSL加密演算法是否存在 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post資料
curl_setopt($ch, CURLOPT_POST, 1);
// post的變數
curl_setopt($ch, CURLOPT_POSTFIELDS, $menuData);
$output = curl_exec($ch);
curl_close($ch);

var_dump($output);  //列印獲得的資料

?>