您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
基于GuzzleHttp(thinkphp5实现原生获取獲取微信公衆平臺jssdk)
发布时间:2022-01-18 22:23:52编辑:雪饮阅读()
具体实现
public function getticket($appid,$secret){
$key="wx_getLocation_ticket";
$ticket=Cache::store('redis')->get($key);
if($ticket){
return $ticket;
}
else{
$access_token=$this->get_access_token($appid,$secret);
$client = new \GuzzleHttp\Client();
$request_res = $client->request('GET', 'https://api.weixin.qq.com/cgi-bin/ticket/getticket', [
'query' => ['access_token' => $access_token,'type'=>"jsapi"]
]);
$body=(string)$request_res->getBody();
$json_arr=json_decode($body,true);
$ticket=$json_arr["ticket"];
Cache::store('redis')->set($key,$ticket,7200);
return $ticket;
}
}
public function get_access_token($appid,$secret){
$key="wx_getLocation_access_token";
$access_token=Cache::store('redis')->get($key);
if($access_token){
return $access_token;
}
else{
$client = new \GuzzleHttp\Client();
$request_res = $client->request('GET', 'https://api.weixin.qq.com/cgi-bin/token', [
'query' => ['grant_type' => 'client_credential','appid'=>$appid,"secret"=>$secret]
]);
$body=(string)$request_res->getBody();
$json_arr=json_decode($body,true);
$access_token=$json_arr["access_token"];
Cache::store('redis')->set($key,$access_token,7200);
return $access_token;
}
}
public function getJssdk(){
$params["url"]=$this->request->post("url");
$validate = new H5GetJssdkValidate();
if (!$validate->check($params)) {
$this->error(__($validate->getError()));
}
$appid=Db::name("config")
->where("group","wechat_public_platform")
->where("name","appid")
->value("value");
$secret=Db::name("config")
->where("group","wechat_public_platform")
->where("name","secret")
->value("value");
$ticket=$this->getticket($appid,$secret);
$signBefore["noncestr"]=time();
$signBefore["jsapi_ticket"]=$ticket;
$signBefore["timestamp"]=time();
//"https://shengke.bhzlkj.com/test/index.html"
$signBefore["url"]=$params["url"];
ksort($signBefore);
$stringA="";
foreach($signBefore as $key=>$val){
$stringA.=$key."=".$val."&";
}
$stringA2=rtrim($stringA,"&");
$stringA3=sha1($stringA2);
Log::write('index2 signBefore:'.var_export($signBefore,true),'notice');
$response["appId"]=$appid;
$response["timestamp"]=$signBefore["timestamp"];
$response["noncestr"]=$signBefore["noncestr"];
$response["signature"]=$stringA3;
$this->success("success",$response,200);
}
关键字词:GuzzleHttp,thinkphp5,jssdk,微信公衆平臺