qq_T_T若是人间四月天_0
2017-04-17 19:15
/*
$url 请求地址
$type 请求方式
$res 返回数据的类型
$arr post传递的参数
*/
public function http_curl($url,$type='get',$res='json',$arr=''){
//通过php的curl工具采集某个url的内容
//1.初始化curl
$ch = curl_init();
//2.设置请求选项, 包括具体的url,默认为get请求方式
curl_setopt($ch,CURLOPT_URL,$url);//需要获取的URL地址,也可以在curl_init()函数中设置
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
//3.采集内容
if($type == 'post'){
curl_setopt($ch,CURLOPT_POST,1);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
}
$response = curl_exec($ch);//执行一个cURL会话
//4.关闭CURL会话
curl_close($ch);
if($res=='json'){
if (curl_errno($url)) {
echo "cURL Error #:" . curl_error($url);
} else {
return json_decode($response,true);
}
};
}
public function getWXAccessToken(){
//从session/cookie中获取accessToken或通过curl获取acceess_token并存入session
if($_SESSION['acceess_token'] && $_SESSION['expire_time']>time()){
//session中有acceess_token并且未过期
return $_SESSION['acceess_token'];
}else{
$appid="wxa22f6d2e559d8dcb";
$appsecret="35e42cd5420e4c90fc2dc6e53cb04ec8";
$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['acceess_token'] = $access_token;
$_SESSION['expire_time'] = time() + 7200;
return $access_token;
}
}
public function defineMenu(){
//自定义菜单
//微信接口通过curl post/get链接
header("Content-type: text/html; charset=utf-8");
$access_token = $this -> getWXAccessToken();
$url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token."";
$postArr = array(
"button" => array(
"type" => "click",
"name" => urlencode("今时天气"),
"key"=> "WEATHER"
),//一级菜单一
array(
"name" =>urlencode("更多"),
"sub_button" => array(
array(
"type" => "view",
"name" => urlencode("我的微店"),
"url" => "https://weidian.com/?userid=1203450123&wfr=c&ifr=shopdetail"
),
array(
"type" => "miniprogram",
"name" => "wxa",
"url" => "http://mp.weixin.qq.com",
"appid" => "wx286b93c14bbf93aa",
"pagepath" => "pages/lunar/index.html"
),
array(
"type" => "click",
"name" => urlencode("赞一下我们"),
"key" => "tuwen"
)
)
)
);
$postJSON = urldecode(json_encode($postArr)) ;
echo $res = $this -> http_curl($url,'post','json', $postArr);
}
同问,兄弟你解决了吗
有个时间好吗
PHP微信公众平台开发高级篇—自定义菜单
28920 学习 · 81 问题
相似问题
回答 1