dlhmaster/extend/weixin/Getopenid.php
2024-06-06 11:06:09 +08:00

149 lines
5.5 KiB
PHP
Raw 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
/**
* Created by PhpStorm.
* Muser: xc
* Date: 2021/9/17
* Time: 16:31
*/
namespace weixin;
class Getopenid
{
private $app_id = 'wx586d77b9edb0636d';
private $app_secret = '1b023b1fb15a005e4a171117afe57b44';
private $state = 1;
/**
* 获取微信授权链接
*
* @param string $redirect_uri 跳转地址
* @param mixed $state 参数
*/
/**
* 获取微信openid
*/
public function getOpenid($turl)
{
if (!isset($_GET['code'])) {
//触发微信返回code码
header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->app_id.'&redirect_uri='.$turl.'&&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
exit();
} else {
//获取code码,以获取openid
$code = $_GET['code'];
$access_info = $this->get_access_token($code);
return $access_info;
}
}
/**
* 获取授权token网页授权
*
* @param string $code 通过get_authorize_url获取到的code
*/
public function get_access_token($code = '')
{
$appid = $this->app_id;
$appsecret = $this->app_secret;
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";
//echo $token_url;
$token_data = $this->http($token_url);
// var_dump( $token_data);
if ($token_data[0] == 200) {
$ar = json_decode($token_data[1], TRUE);
return $ar;
}
return $token_data[1];
}
public function getAcc($code = ''){
$appid = $this->app_id;
$appsecret = $this->app_secret;
$token_url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=authorization_code";
$token_data = $this->http($token_url);
if ($token_data[0] == 200) {
$ar = json_decode($token_data[1], TRUE);
return $ar;
}
return $token_data[1];
}
public function getToken(){
$appid = $this->app_id;
$appsecret = $this->app_secret;
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$token_data = $this->http($token_url);
if ($token_data[0] == 200) {
$ar = json_decode($token_data[1], TRUE);
$this->send($ar['access_token']);
}
return $token_data[1];
}
// 公众号一次性订阅消息
public function send($access_token){
$token_url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;
$data = [
'template_id' => 'kKgDn4gfOLtH9KFbqQcCd4VZN03FOoUYoCKciIhgvks', // 所需下发的订阅模板id
'touser' => 'ouOMm41xnIKqoZ66ygwJa-qJQpgw', // 接收者(用户)的 openid
'kKgDn4gfOLtH9KFbqQcCd4VZN03FOoUYoCKciIhgvks'=>'developer',
'page' => '', // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
'amount1' => [
'value' => '10.00',
],
'character_string2' => [
'value' => 11111,
],
'date3' => [
'value' => "2019-10-17 11:19:30",
],
'thing4' => [
'value' => "测试详情",
],
'thing7' => [
'value' => "姓名-手机号码-地址",
],
],
];
$data = json_encode($data);
$token_data = $this->http($token_url,'POST',$data);
var_dump($token_data);
}
public function http($url, $method = '', $postfields = null, $headers = array(), $debug = false)
{
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
}
// 本地调试
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
// 本地调试
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
$response = curl_exec($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo '=====info=====' . "\r\n";
print_r(curl_getinfo($ci));
echo '=====$response=====' . "\r\n";
print_r($response);
}
curl_close($ci);
return array($http_code, $response);
}
}