With the rise of social media, WeChat Official Accounts have become an essential tool for businesses to interact with users. To facilitate user engagement, businesses often use QR codes to encourage users to follow their official accounts. In this article, we'll show you how to develop QR code generation for WeChat Official Accounts using PHP, with specific code examples to help you get started quickly.
Before starting development, the first step is to obtain the QR code generation URL. This can be done using the API provided by the WeChat Official Platform. Below is a PHP code example to obtain the QR code generation URL:
<?php
$appid = "your_app_id"; // Your Official Account AppID
$secret = "your_app_secret"; // Your Official Account AppSecret
$access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$response = file_get_contents($access_token_url);
$result = json_decode($response, true);
$access_token = $result['access_token'];
$qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
?>
In this code, $appid and $secret represent the AppID and AppSecret of your Official Account. The access_token is first obtained by calling the WeChat API. Then, this token is used to generate the QR code.
Once the QR code generation URL is obtained, we can use PHP's imagecreatefromstring and imagepng functions to generate the QR code and save it as an image. Below is the PHP code for generating and saving the QR code image:
<?php
$qrcode_data = array(
'expire_seconds' => 604800, // QR code expiration time, set to 7 days
'action_name' => 'QR_SCENE',
'action_info' => array(
'scene' => array(
'scene_id' => 1234 // QR code parameter, customizable
)
)
);
$qrcode_json = json_encode($qrcode_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => $qrcode_json
)
);
$context = stream_context_create($options);
$qrcode_response = file_get_contents($qrcode_url, false, $context);
$qrcode_result = json_decode($qrcode_response, true);
$qrcode_ticket = $qrcode_result['ticket'];
$qrcode_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($qrcode_ticket);
$qrcode_image = imagecreatefromstring(file_get_contents($qrcode_url));
imagepng($qrcode_image, 'qrcode.png'); // Save the QR code as an image
?>
In this code, we use json_encode to convert the QR code data into JSON format. Then, using stream_context_create, we send an HTTP request to obtain the QR code image URL. Finally, imagecreatefromstring and imagepng are used to generate and save the QR code image locally.
This article has shown you how to develop QR code generation for WeChat Official Accounts using PHP, along with specific code examples for obtaining the QR code generation URL, generating the QR code image, and saving it. With these simple steps, you can easily generate QR codes for your WeChat Official Account to attract more users to follow your account. We hope this tutorial helps you enhance your account's user engagement.