dlh_test_server/extend/weixin/Getgzopenid.php

151 lines
5.6 KiB
PHP
Raw Normal View History

2024-08-03 10:04:14 +08:00
<?php
/**
* Created by PhpStorm.
* Muser: xc
* Date: 2021/9/17
* Time: 16:31
*/
namespace weixin;
class Getgzopenid
{
private $app_id = 'wxc83f183e2cce3182';
private $app_secret = '67d9d0f2d2b27679cd038c0b0fe15f33';
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;
$api_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$token_data = $this->http($api_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 getToken($address,$user_msg,$goods_msg){
$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->sendWeixingongzhonghao($ar['access_token'],$address,$user_msg,$goods_msg);
}
return $token_data[1];
}
2024-08-03 10:04:14 +08:00
public function sendWeixingongzhonghao($access_token,$address,$user_msg,$goods_msg){
$token_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$max_length = 20;
$replacement = '...';
2024-08-03 10:04:14 +08:00
foreach($goods_msg as $key=>$value){
foreach($value['goodscontact'] as $k=>$v){
// TODO 暂时将openid写死,因为后端在这一块设计的不好,这里的open_id是张姐的
$str = $value['goods_name'].",数量:".$value['goods_num'];
if (mb_strlen($str) > $max_length) {
$str = mb_substr($str, 0, $max_length - mb_strlen($replacement)) . $replacement;
}
2024-08-03 10:04:14 +08:00
$data=[
"touser" => $v['open_id'] ? $v['open_id'] : 'oUzra6HlpaARciJpUk8eeUxFayPk' ,
"template_id"=>"oWpOaEVOgiORQ4bDj9d5u98YMhWWyuShrxECO5RKBW8",
"data"=>[
"thing2"=>["value"=>$user_msg['contact']],
"phone_number3"=>["value"=>$user_msg['telephone']],
"thing4"=>["value" => $str],
"thing5"=>["value" => $address],
2024-08-03 10:04:14 +08:00
"time6"=>["value"=>date("Y-m-d H:i:s")],
]
];
$data = json_encode($data);
$token_data = $this->http($token_url,'POST',$data);
}
}
}
2024-08-03 10:04:14 +08:00
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);
}
}