因为公司H5需要用到微信分享,需要保存access_token ,且框架为自用框架,所以自己弄了个简单的分享来实现。
之前说过微信的一些问题,这里不重复说明了。开发微信注意几点
微信开发需要access_token时效7200秒,每天最多请求2000次,重新请求后上次的5分钟内时效
微信分享需要jsapi_ticket时效7200秒,需要access_token 请求
微信分享之后会再URL后面补上分享参数,vue或者其他前端框架需要特别注意这里
下面是自己的代码:
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/12/22 0022
* Time: 上午 10:05
*/
class wxshare
{
public function __construct()
{
$this->app_id = "";
$this->app_secret = "";
}
public function get_access_token()
{
$token_info = M('wx_config')->find("id = 1"); //根据自己框架封装的查询方法查询微信数据
// 判断token 是否过期
if(empty($token_info['access_expire_time']) || $token_info['access_expire_time'] < time()){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->app_id&secret=$this->app_secret";
$res = json_decode($this->http_get($url),true);
//$res = '{"access_token":"5_PGyp5NcHtq3-McBgMK9p6FFvyEaZBcIutvS80XVQlUWb-6U-gsDmBZaOpw4rFzWTlhjY4YmyqnRVhN-Y1A85bCq-DCDd48hNJ9nkkhJYq7oqGrUeaAq8ZTZ2p6v8qmpgzuBL-v7IqVCe84-rPOWbBGAIHP","expires_in":7200}';
//$res = json_decode($res,true);
if($res['access_token']){
$data = array(
"access_token" => $res['access_token'],
"access_expire_time" => time()+7000
);
M('wx_config')->update($data,"id = 1");
return $res['access_token'];
}else{
@file_put_contents(DIR.'/www/log/wxshare_'.date('Ymd'),"获取ACCESS_TOKEN 错误".$res['errmsg'].PHP_EOL, FILE_APPEND);// 根据自己框架写入失败日志
}
}else{
return $token_info['access_token'];
}
}
public function get_jsapi_token()
{
$token_info = M('wx_config')->find("id = 1");//根据自己框架封装的查询方法查询微信数据
$access_token = $this->get_access_token();
// 判断token 是否过期
if(empty($token_info['jsapi_token']) || $token_info['jsapi_expire_time'] < time()){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$access_token;
$res = json_decode($this->http_get($url),true);
//$res = '{"errcode":0,"errmsg":"ok","ticket":"sM4AOVdWfPE4DxkXGEs8VJlw7gitBGlUZ-yanxWxmVRr1ugcM9aUKaXlqFQwFbSum-DA-MeQBestUGwUsgZtJA","expires_in":7200}';
//$res = json_decode($res,true);
if($res['errcode'] == 0){
$data = array(
"jsapi_token" => $res['ticket'],
"jsapi_expire_time" => time()+7000
);
M('wx_config')->update($data,"id = 1");
return $res['ticket'];
}else{
@file_put_contents(DIR.'/www/log/wxshare_'.date('Ymd'),"获取JSAPI_TOKEN 错误".$res['errmsg'].PHP_EOL, FILE_APPEND);// 失败日志
}
}else{
return $token_info['jsapi_token'];
}
}
public function http_get($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
public function create_nonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
public function get_sign_package($url) {
$jsapi_token = $this->get_jsapi_token();
$timestamp = time();
$nonceStr = $this->create_nonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=".$jsapi_token."&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$token_info = M('wx_config')->find("id = 1");//根据自己框架封装的查询方法查询微信数据
$signPackage = array(
"access_token" => $token_info['access_token'],
"jsapi_token" => $token_info['jsapi_token'],
"appId" => $this->app_id,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
public function share_api()
{
$postdata = $_REQUEST;
$signPackage = $this->get_sign_package($postdata['url']);
$sharedata['title'] = $postdata['title'];
$sharedata['desc'] = $postdata['desc'];
$sharedata['link'] = $postdata['url'];
$sharedata['imgUrl'] = $postdata['imgUrl'];
$sharedata['share'] = $signPackage;
echo json_encode($sharedata);
}
}
因为接口封装之后请求URL地址为当前JS请求时发送,然后根据传参数请求后返回JSON数据处理,利用微信的JS进行后续唤醒操作
微信分享文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
上一篇: 百度编辑器上传图片保存全路径...
下一篇: 心灵毒鸡汤...