首頁  >  文章  >  後端開發  >  在PHP中實作微信二維碼生成

在PHP中實作微信二維碼生成

WBOY
WBOY原創
2023-05-13 16:01:541678瀏覽

隨著行動網路的發展和普及,微信已經成為了人們生活和工作中不可或缺的一部分。為了滿足用戶的需求,微信也不斷推出新的功能,其中最為重要的是微信支付。而為了使用微信支付,必須要有一個可靠的二維碼產生功能,本文將介紹如何在PHP中實作微信二維碼生成。

首先,我們要先明確一點,微信二維碼產生分成兩種方式,一種是永久性二維碼,另一種是臨時性二維碼。永久性二維碼只有在使用者主動進行操作時才會失效,例如在商城中的某個頁面展示永久性二維碼,當使用者掃描二維碼並進行購買後,二維碼才會失效。而臨時性二維碼則會在一定時間內失效,一般情況下為30分鐘。

接下來,我們將分別講解如何在PHP中實現永久性二維碼和臨時性二維碼的生成。

一、永久性二維碼的生成

實現永久性二維碼的產生需要呼叫微信支付中的二維碼接口,以下是二維碼接口的呼叫方式:

//引入Vendor下的自动加载文件
require_once("Vendor/autoload.php");

//设置appid、secret、商户号等信息
$appid = '';  //微信开放平台appid
$appsecret = ''; //微信开放平台appsecret
$merchant_id = '';  //商户号
$key = '';  //API密钥

//构造二维码接口调用参数
$api_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN';
$access_token = '';  //access_token需要通过调用微信开放平台的token接口获取
$expire_seconds = '';  //永久性二维码不需要该参数
$action_info = ['scene'=>['scene_id'=>1001]];  //永久性二维码的参数形式

//获取access_token
function getAccessToken()
{
    $appid = '';  //微信开放平台appid
    $appsecret = ''; //微信开放平台appsecret
    $api_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
    $api_result = file_get_contents($api_url);
    $api_result_arr = json_decode($api_result, true);
    return $api_result_arr['access_token'];
}

$access_token = getAccessToken();

//构造请求参数
$data = array(
    'expire_seconds' => $expire_seconds,
    'action_name' => 'QR_LIMIT_SCENE',
    'action_info' => $action_info
);

$json_data = json_encode($data);

//构造请求头
$header = array(
    'Content-Type: application/json;charset=utf-8',
    'Content-Length: ' . strlen($json_data)
);

//发送POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, str_replace('ACCESS_TOKEN', $access_token, $api_url));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
curl_close($ch);

//对响应数据进行处理
$json_obj = json_decode($response, true);
$ticket = $json_obj['ticket'];
$qrcode_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket);

二、臨時性二維碼的生成

臨時性二維碼的生成同樣需要調用微信支付中的二維碼接口,並且需要在接口調用參數中指定expire_seconds參數。以下是臨時性二維碼介面的呼叫方式:

//引入Vendor下的自动加载文件
require_once("Vendor/autoload.php");

//设置appid、secret、商户号等信息
$appid = '';  //微信开放平台appid
$appsecret = ''; //微信开放平台appsecret
$merchant_id = '';  //商户号
$key = '';  //API密钥

//构造二维码接口调用参数
$api_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN';
$access_token = '';  //access_token需要通过调用微信开放平台的token接口获取
$expire_seconds = 1800;  //临时性二维码的有效时间为30分钟
$action_info = ['scene'=>['scene_id'=>1001]];  //临时性二维码的参数形式

//获取access_token
function getAccessToken()
{
    $appid = '';  //微信开放平台appid
    $appsecret = ''; //微信开放平台appsecret
    $api_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
    $api_result = file_get_contents($api_url);
    $api_result_arr = json_decode($api_result, true);
    return $api_result_arr['access_token'];
}

$access_token = getAccessToken();

//构造请求参数
$data = array(
    'expire_seconds' => $expire_seconds,
    'action_name' => 'QR_SCENE',
    'action_info' => $action_info
);

$json_data = json_encode($data);

//构造请求头
$header = array(
    'Content-Type: application/json;charset=utf-8',
    'Content-Length: ' . strlen($json_data)
);

//发送POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, str_replace('ACCESS_TOKEN', $access_token, $api_url));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
curl_close($ch);

//对响应数据进行处理
$json_obj = json_decode($response, true);
$ticket = $json_obj['ticket'];
$qrcode_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket);

以上就是在PHP中實作微信二維碼產生的方法,需要注意的是,在實際開發中,我們還需要在商家平台中產生公眾號付款訊息,並且需要進行相關的驗簽和安全性處理才能確保二維碼安全可靠。

以上是在PHP中實作微信二維碼生成的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn