更新公众号菜单

刘德胜 428次浏览

摘要:更新公众号菜单与获取素材<?php header("Content-type:text/html;charset=utf-8"); //订阅号 define('APPID','333'); define('APPSECRET','888'); func...

更新公众号菜单与获取素材

<?php
header("Content-type: text/html; charset=utf-8");
//订阅号
define('APPID','333');
define('APPSECRET','888');

function httpsirequest($url){
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec($curl);
  if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
  curl_close($curl);
  return $data;
}

function get_access_token(){    
	$new_access_token_json  = httpsirequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET);
	$new_access_token_array = json_decode($new_access_token_json, true);
	return $new_access_token_array['access_token'];    
}
$access_token =  get_access_token();

define("ACCESS_TOKEN", $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;
// }

// $data = '{
//   "button":[
//   {
//    "type":"view",
//    "name":"网站首页",
//    "url":"http://www.hebazi.net"
//   },
//   {
//    "type":"view",
//    "name":"预测工具",
//    "url":"http://www.hebazi.net"
//   },
//   {
//     "type": "view_limited", 
//     "name": "预测说明", 
//     "media_id": "tq0tIIVswHVqq2_aTetECrTLpaEGC1jQqXqxblNuUwU"
//   }]
// }';
// echo createMenu($data);


//获取素材
function getMenu($data){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/material/batchget_material?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;
}


$data = '
{
  "type":"news",
  "offset":0,
  "count":5
}
';
$str = getMenu($data);
print_r($str);