<?php $appid = "wxbbae0ae85ac85399"; $appsecret = "7252f9a7d06601e6f57d7378b36672f2"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $output = https_request($url); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"]; //var_dump($access_token); $jsonmenu = '{ "button":[ { "type":"view", "name":"首页", "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbbae0ae85ac85399&redirect_uri=http://www.jiesen365.com/oauth.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect" }, { "type":"view", "name":"约课", "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbbae0ae85ac85399&redirect_uri=http://www.jiesen365.com/oauth.php&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect" }, { "type":"view", "name":"我的", "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbbae0ae85ac85399&redirect_uri=http://www.jiesen365.com/oauth.php&response_type=code&scope=snsapi_userinfo&state=3#wechat_redirect" }] }'; $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; $result = https_request($url, $jsonmenu); var_dump($result); function https_request($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } ?>
Oauth 使用
<?php define('PHPCMS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR); include PHPCMS_PATH.'/phpcms/base.php'; if (isset($_GET['code'])){ $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxbbae0ae85ac85399&secret=7252f9a7d06601e6f57d7378b36672f2&code=".$_GET['code']."&grant_type=authorization_code"; $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); $output = curl_exec($curl); curl_close($curl); $output = json_decode($output,true); // echo $output['openid']; //从微信获取到了用户openid $third_party = pc_base::load_model('third_party_model'); $wfinfo = $third_party->get_one(array('wid'=>$output['openid']),'userid'); if($wfinfo[userid]){ switch($_GET[state]){ case 1: Header("HTTP/1.1 303 See Other");Header("Location: http://www.jiesen365.com/index.php?m=mobilecenter&c=index&a=init&wxid=".$output['openid']); break; case 2: Header("HTTP/1.1 303 See Other");Header("Location: http://www.jiesen365.com/index.php?m=mobilecenter&c=index&a=order&wxid=".$output['openid']); break; case 3: Header("HTTP/1.1 303 See Other");Header("Location: http://www.jiesen365.com/index.php?m=mobilecenter&c=index&a=self&wxid=".$output['openid']); break; } }else{ $html = "<div style='300px;height:300px;font-size:24px;margin:50% 0 0 40%;'><a style='font-size:24px;' href='http://www.jiesen365.com/index.php?m=mobilecenter&c=index&a=login&wxid=".$output['openid']."'>点击绑定</a><p style='font-size:24px;'>,快速绑定杰森英语账号啦~享受小杰给你提供的微信约课神器!来嘛~约嘛~</p></div>"; echo $html; } }else{ echo "NO CODE"; } ?>