微信自定义菜单的创建/查询/取消php示例代码

前端之家收集整理的这篇文章主要介绍了微信自定义菜单的创建/查询/取消php示例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

微信公众帐号 服务号可以使用 自定义菜单功能。之前在创建菜单时一直失败,原因是$data 格式一直没有传正确,后来终于解决了。这里先记录下 顺便封装了一个类,便于自定义菜单的管理。此类仅仅是自定义菜单的管理类,并未涉及微信自定义回复菜单事件的代码

代码如下

public static $appid = null; // 申请得到的appid

public static $secret = null; // 申请得到的secret

public static $getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

public static $createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";

public static $selMenu ="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";

public static $delMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";

public static $opt = array(
CURLOPT_SSL_VERIFYPEER => false,CURLOPT_SSL_VERIFYHOST => false,CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',CURLOPT_FOLLOWLOCATION => 1,CURLOPT_AUTOREFERER =>1,CURLOPT_RETURNTRANSFER => true
);

public $ACCESS_TOKEN = null;

/**

  • 创建菜单
    */
    public function create()
    {

$this->token();

$strMeau = '{
"button":[
{
"type":"click","name":"菜单左","key":"V_MENU_LEFT"
},{
"type":"click","name":"菜单中","key":"V_MENU_CENTER"
},{
"type":"click","name":"菜单右","key":"V_MENU_RIGHT"
}]
}';

$ret = $this->HttpPost(self::$createMenu.$this->ACCESS_TOKEN,self::$opt,$strMeau);

echo $ret;
}

/**

$ret = $this->HttpGet(self::$selMenu.$this->ACCESS_TOKEN,self::$opt);

echo $ret;
}

/**

  • 取消菜单
    */
    public function del()
    {
    $this->token();

$ret = $this->HttpGet(self::$delMenu.$this->ACCESS_TOKEN,self::$opt);

echo $ret;
}

/**

  • 获取token
    */
    private function token()
    {
    $tokenUrl = self::$getToken."&appid=".self::$appid."&secret=".self::$secret;
    $ret = $this->HttpGet($tokenUrl,self::$opt);

$arrRet = json_decode($ret,true);

$this->ACCESS_TOKEN = $arrRet['access_token'];
}

/**

  • POST 模式
  • @param string $url post 的地址
  • @param array $opt post 选项
  • @param array $post_data post 数据
  • @return mixed
    */
    private function HttpPost($url,$opt = array(),$post_data)
    {
    $setopt = array(
    CURLOPT_HEADER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_URL => $url,CURLOPT_CUSTOMREQUEST => 'POST',CURLOPT_POST => 1,CURLOPT_POSTFIELDS => $post_data,);

if ( !empty($opt) ) {
foreach ($opt as $key => $value) {
$setopt[$key] = $value;
}
}

$curl = curl_init($url);

foreach ($setopt as $key => $value) {
curl_setopt($curl,$key,$value );
}

$responseText = curl_exec($curl);

curl_close($curl);

return $responseText;
}

/**

  • GET 方式
  • @param stinrg $url GET 的url
  • @param array $opt GET 的选项
  • @return mixed
    */
    private function HttpGet($url,$opt = array())
    {
    $setopt = array(
    CURLOPT_HEADER => 0,CURLOPT_URL => $url
    );

if ( !empty($opt) ) {
foreach ($opt as $key => $value) {
$setopt[$key] = $value;
}
}

$curl = curl_init($url);

foreach ($setopt as $key => $value) {
curl_setopt($curl,$value );
}

$responseText = curl_exec($curl);

curl_close($curl);

return $responseText;
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

猜你在找的PHP相关文章