我在網頁上出現的結果和視屏中一樣,可在微信手機端就是不出現菜單,求助,問題在哪??
MjyxwFWzRV8r4Nw_wUj51fUjfg-dzmKsMKdaweUfd7t3-GYg106H86j3Do_f3vyayfMn7sbcj-wTEhHDFh70to-1KwKpEYhYM1HT-G-izwFAVRJJ1cDsWggubCixgxJEDNEfABARBS
https://api.weixin.qq.com/cgi-bin/menu/create?access_token=MjyxwFWzRV8r4Nw_wUj51fUjfg-dzmKsMKdaweUfd7t3-GYg106H86j3Do_f3vyayfMn7sbcj-wTEhHDFh70to-1KwKpEYhYM1HT-G-izwFAVRJJ1cDsWggubCixgxJEDNEfABARBSarray(1) { ["button"]=> array(3) { [0]=> array(3) { ["name"]=> string(27) "%E8%8F%9C%E5%8D%95%E4%B8%80" ["type"]=> string(5) "click" ["key"]=> string(5) "item1" } [1]=> array(2) { ["name"]=> string(27) "%E8%8F%9C%E5%8D%95%E4%BA%8C" ["sub_button"]=> array(3) { [0]=> array(3) { ["name"]=> string(18) "%E6%AD%8C%E6%9B%B2" ["type"]=> string(5) "click" ["key"]=> string(5) "songs" } [1]=> array(3) { ["name"]=> string(18) "%E7%94%B5%E5%BD%B1" ["type"]=> string(4) "view" ["url"]=> string(20) "http://www.baidu.com" } [2]=> array(3) { ["name"]=> string(9) "%E6%AD%8C" ["type"]=> string(4) "view" ["url"]=> string(17) "http://www.qq.com" } } } [2]=> array(3) { ["name"]=> string(18) "%E5%9B%BE%E7%89%87" ["type"]=> string(4) "view" ["url"]=> string(18) "http://www.163.com" } } }{"button":[{"name":"菜單一","type":"click","key":"item1"},{"name":"菜單二","sub_button":[{"name":"歌曲","type":"click","key":"songs"},{"name":"電影","type":"view","url":"http:\/\/
部分代碼:
function http_curl($url,$type='get',$res='json',$arr=''){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
if($type=='post'){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
}
$output=curl_exec($ch);
curl_close($ch);
if($res=='json'){
if(curl_errno($ch)){
return curl_error($ch);
}else{
return json_decode($output,true);
}
}
}
? ??
public function getWxAccessToken(){
if($_SESSION['access_token'] && $_SESSION['expire_time']>time()){
return $_SESSION['access_token'];
}else{
? ?$appid='wxb7a1e21ed61a90a2';
$appsecret='d4624c36b6795d1d99dcf0547af5443d';
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res=$this->http_curl($url,'get','json');
$access_token=$res['access_token'];
$_SESSION['access_token']=$access_token;
$_SESSION['expire_time']=time()+7000;
return $access_token;
}
}
? ? function definedItem(){
header('Content-type: text/html; charset=utf-8');
echo $access_token=$this->getWxAccessToken();
? ? echo '<br />';
echo $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$postArr=array(
? 'button'=> array(
? ? ? ?array(
'name'=>urlencode('菜單一'),
'type'=>'click',
'key'=>'item1',
),
??
array(
'name'=>urlencode('菜單二'),
'sub_button'=>array(
array(
'name'=>urlencode('歌曲'),
'type'=>'click',
'key'=>'songs',
),
array(
'name'=>urlencode('電影'),
'type'=>'view',
'url'=>'http://www.baidu.com',
),
array(
'name'=>urlencode('歌'),
'type'=>'view',
'url'=>'http://www.qq.com',
),
),
?
),
array(
'name'=>urlencode('圖片'),
'type'=>'view',
'url'=>'http://www.163.com',
),
? ? ? ? ? ? ),
);
echo '<hr/>';
? ? ? ? var_dump($postArr);
? ?echo '<hr/>';
? ? ? ??
echo $postJson=urldecode(json_encode($postArr));
$res=$this->http_curl($curl,'post','json',$postJson);
? ?echo '<hr/>';
var_dump($res);
}
2016-09-09
function getWxAccessToken()
{
? ?session_start();
? ?//header("Content-Type:text/html;charset=utf-8");
? ?$time=time();
? ?if ($_SESSION['accessToken'] && $time<$_SESSION['expirytime']) {
? ? ? ?return $this->accessToken=$_SESSION['accessToken'];
? ?}
? ?$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->appsecret;
? ?$res = $this->http_curl($url);
? ?if ($res) {
? ? ? ?$tokenArray = json_decode($res, true);
? ? ? ?if ($tokenArray['access_token']) {
? ? ? ? ? ?$_SESSION['accessToken']=$tokenArray['access_token'];
? ? ? ? ? ?$_SESSION['expirytime']=time()+7000;
? ? ? ? ? ?return $this->accessToken = $_SESSION['accessToken'];
? ? ? ?}
? ?} else {
? ? ? ?return false;
? ?}
}
應該是token的問題,你把if ($_SESSION['accessToken'] && $time<$_SESSION['expirytime']) {
? ? ? ?return $this->accessToken=$_SESSION['accessToken'];
? ?}這句給注釋掉,先不用session,通過http請求獲取token就可以了