public function menus(){
//创建菜单
$access_token = $this->get_accessToken();
$url= "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$menus = ["button"=>array(
array(
"name"=>"********",
"sub_button"=>array(
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
),
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
),
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
),
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
),
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
)
)
),
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
),
array(
"name"=>"******",
"sub_button"=>array(
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
),
array(
"type"=>"view",
"name"=>"****",
"url"=>"****"
)
)
)
) ];
$output = $this->wx_post($url,json_encode($menus,JSON_UNESCAPED_UNICODE));
if($output == 0){
echo($output);
}
return false;
}
public function get_accessToken()
{
if(count($this->RedisConfig)==0){
return 'api return error';
}
$Redis=new Redis($this->RedisConfig);
$wx_access_token = $Redis->get("GZ_access_token");
if(!($wx_access_token===false)){/* 在有效期,直接返回access_token */
return $wx_access_token;
}else{/* 不在有效期,重新发送请求,获取access_token */
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->WXPAppID.'&secret='.$this->WXPAppSecret;
$result = $this->wx_get($url);
if(strlen($result['content'])>10){
$res = json_decode($result['content'],true); //json字符串转数组
if(array_key_exists("errcode", $res)){
return $res['errmsg'];
}elseif (array_key_exists("access_token", $res)){
$Redis->set("GZ_access_token",$res['access_token'],7100);
return $res['access_token'];
}else{
return 'api return error';
}
}else{
return 'api return errors';
}
}
}
public function wx_get($url){
$parm = [
'url' => $url
];
return curl_send($parm);
}
public function wx_post($url,$data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function curl_send($rq) {
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $rq['url']);
switch (true) {
case isset($rq['method']) && in_array(strtolower($rq['method']), array('get', 'post', 'put', 'delete', 'head')):
$method = strtoupper($rq['method']);
break;
case isset($rq['data']):
$method = 'POST';
break;
default:
$method = 'GET';
}
$header = isset($rq['header']) ? $rq['header'] : array();
$header[] = 'Method:' . $method;
$header[] = "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
isset($rq['host']) && $header[] = 'Host:'.$rq['host'];
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
isset($rq['timeout']) && curl_setopt($curlHandle, CURLOPT_TIMEOUT, $rq['timeout']);
isset($rq['data']) && in_array($method, array('POST', 'PUT')) && curl_setopt($curlHandle, CURLOPT_POSTFIELDS, http_build_query($rq['data']));
$ssl = substr($rq['url'], 0, 8) == "https://" ? true : false;
if( isset($rq['cert'])){
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($curlHandle, CURLOPT_CAINFO, $rq['cert']);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST,2);
if (isset($rq['ssl_version'])) {
curl_setopt($curlHandle, CURLOPT_SSLVERSION, $rq['ssl_version']);
} else {
curl_setopt($curlHandle, CURLOPT_SSLVERSION, 4);
}
}else if( $ssl ){
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER,false); //true any ca
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST,1); //check only host
if (isset($rq['ssl_version'])) {
curl_setopt($curlHandle, CURLOPT_SSLVERSION, $rq['ssl_version']);
} else {
curl_setopt($curlHandle, CURLOPT_SSLVERSION, 4);
}
}
$return['content'] = curl_exec($curlHandle);
$return['result'] = curl_getinfo($curlHandle);
curl_close($curlHandle);
return $return;
}