Current Location: Home> Latest Articles> How to Implement Share Function in WeChat Mini Program Using EasyWeChat and PHP

How to Implement Share Function in WeChat Mini Program Using EasyWeChat and PHP

M66 2025-06-11

1. Preparation Work

Before we start, we need to prepare some basic development environment and materials:
  1. WeChat Official Account: Register a WeChat Official Account and create a Mini Program.
  2. EasyWeChat: Install the EasyWeChat library, which provides many convenient features for interacting with WeChat APIs.
  3. PHP Environment: Set up the PHP environment and install the Composer package manager.

2. Configuring EasyWeChat

First, we need to include EasyWeChat in the project. You can easily install EasyWeChat using Composer by running the following command in the project root directory:
composer require overtrue/wechat

After the installation, include the EasyWeChat autoload file in the project:

require_once 'vendor/autoload.php';

Next, we need to configure EasyWeChat. Create a config.php file in the project root directory and configure it as follows:

<?php
return [
    'app_id' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',
    'token' => 'YOUR_TOKEN',
    'log' => [
        'level' => 'debug',
        'file'  => 'path/to/log.log',
    ],
];

Replace YOUR_APP_ID, YOUR_APP_SECRET, and YOUR_TOKEN with your Mini Program's AppID, AppSecret, and Token. The log configuration is optional and used for logging purposes.

3. Implementing the Share Function

After configuring EasyWeChat, we can start implementing the share function.

1. Getting the access_token

The share function requires the access_token, which can be obtained using the EasyWeChat API. Add the following code in the `config.php` file:
$wechat = new EasyWeChat\Foundation\Application(require_once 'config.php');
$accessToken = $wechat->access_token;
$token = $accessToken->getToken();

2. Generating the Share Link

Using the EasyWeChat API, we can generate a custom share link. The following code generates a link for sharing on Moments:
$shareLink = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' . $token . '&path=pages/index/index&scene=123';

The path parameter specifies the page path of the Mini Program, and the scene parameter specifies the scene value.

3. Calling the WeChat API

Finally, we need to call the WeChat API to perform the share operation. The following code uses PHP's cURL library to send a POST request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $token);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'touser' => 'OPENID',
    'msgtype' => 'news',
    'news' => [
        'articles' => [
            [
                'title' => 'Share Title',
                'description' => 'Share Description',
                'url' => $shareLink,
                'picurl' => 'Share Image URL',
            ],
        ],
    ],
], JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

Replace OPENID with the user's openid, and title, description, url, and picurl with the share title, description, link, and image URL respectively.

4. Conclusion

By combining EasyWeChat and PHP, we can easily implement the share functionality in WeChat Mini Programs. In this article, we have covered how to configure EasyWeChat, obtain the access_token, generate the share link, and call WeChat APIs. We hope these tips will help you in your Mini Program development.