2023-01-29 10:26:52 +08:00

67 lines
3.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 小程序配置
**/
class Wechatapi
{
public function access_token($wxid = 0)
{
$redis = new \Redis();
$redis->connect('127.0.0.1',6379);
$token = $redis->get('AccessToken');
if(!$token){
$app = db('applets_api')->where('wxid',$wxid)->where('number', '<', 400000)->find();
$appid = $app['appid'];
$secret = $app['secret'];
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$token = file_get_contents($url);
dump($url);die;
$token2 = json_decode($token, true);
$redis->setex('AccessToken', 3600, $token2['access_token']);
db('applets_api')->where('appid',$app['appid'])->update(['access_token' => $token2['access_token']]);
return $token2['access_token'];
}
return $token;
}
function get_wxapi_url($wxid = 38)
{
$access_token = $this->access_token();
$url = 'https://api.weixin.qq.com/wxa/generate_urllink?access_token='.$access_token;
$data = '{
"path": "/pages/index/index",
"query": "wxid='.$wxid.'",
"is_expire":true,
"expire_type":1,
"expire_interval":1,
"env_version": "release"
}';
$res = $this->https_request($url,$data);//请求开始
$res2 = json_decode($res, true);
if($res2['errmsg'] == 'ok'){
db('applets_api')->where('access_token',$access_token)->setInc('number',1);
}
return $res2['url_link'];
}
//curl请求函数微信都是通过该函数请求
public function https_request($url, $data = null){
$curl = curl_init(); //创建请求
curl_setopt($curl, CURLOPT_URL, $url); //设置请求的URL
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);//禁用后cURL将终止从服务端进行验证。使用CURLOPT_CAINFO选项设置证书使用CURLOPT_CAPATH选项设置证书目录 如果CURLOPT_SSL_VERIFYPEER(默认值为2)被启用CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);//1 检查服务器SSL证书中是否存在一个公用名(common name)。译者注:公用名(Common Name)一般来讲就是填写你将要申请SSL证书的域名 (domain)或子域名(sub domain)。2 检查公用名是否存在,并且是否与提供的主机名匹配
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1); //设置为POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//传递一个数组到CURLOPT_POSTFIELDScURL会把数据编码成 multipart/form-data而然传递一个URL-encoded字符串时数据会被编码成 application/x-www-form-urlencoded
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);//在启用CURLOPT_RETURNTRANSFER的时候返回原生的Raw输出。
$output = curl_exec($curl);//设置OK
curl_close($curl);
return $output;
}
}