Current Location: Home> Latest Articles> 【Complete Guide to Developing WeChat Mini Program Live Shopping Feature with PHP】

【Complete Guide to Developing WeChat Mini Program Live Shopping Feature with PHP】

M66 2025-06-24

Developing Live Shopping Features in WeChat Mini Programs Using PHP

As e-commerce continues to evolve, live shopping within WeChat Mini Programs has become a major focus for many merchants. This feature allows users to watch live streams while making purchases directly in the app, significantly improving conversion rates. In this article, we’ll explore how to develop the live shopping functionality using PHP, including fetching live room and product data through WeChat APIs.

1. Preparation Requirements

Before diving into PHP development, ensure the following prerequisites are met:

  1. You have a registered WeChat Mini Program account and obtained your AppID.

  2. The live streaming feature is enabled, and you have access to a live room ID.

  3. PHP environment is installed locally, recommended version 7.0 or above.

2. Fetching the Live Room List with PHP

Below is a PHP script that interacts with WeChat’s API to retrieve the list of available live rooms associated with the Mini Program:

$appid = "your_appid"; // Mini Program AppID
$appsecret = "your_appsecret"; // Mini Program AppSecret
$accessToken = "";

// Function to get access_token
function getAccessToken($appid, $appsecret) {
    global $accessToken;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
    $response = file_get_contents($url);
    $result = json_decode($response, true);
    $accessToken = $result["access_token"];
}

// Function to get live room list
function getLiveRooms() {
    global $accessToken;
    $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken}";
    $response = file_get_contents($url);
    $result = json_decode($response, true);
    return $result;
}

// Get live room data
getAccessToken($appid, $appsecret);
$liveRooms = getLiveRooms();

// Display live room information
foreach ($liveRooms["room_info"] as $room) {
    echo "Room ID: {$room["roomid"]}\n";
    echo "Room Title: {$room["name"]}\n";
    echo "Cover Image: {$room["cover_img"]}\n";
}

The getAccessToken function retrieves the API token, while getLiveRooms fetches the list of live rooms using that token.

3. Fetching Products in the Live Room

To display products during the livestream, you need to retrieve product data for a specific live room. Here's the code to accomplish that:

$appid = "your_appid";
$appsecret = "your_appsecret";
$accessToken = "";

// Get access_token
function getAccessToken($appid, $appsecret) {
    global $accessToken;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
    $response = file_get_contents($url);
    $result = json_decode($response, true);
    $accessToken = $result["access_token"];
}

// Get live room product list
function getLiveGoods($roomId) {
    global $accessToken;
    $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/getgoodslist?access_token={$accessToken}";
    $data = [
        "roomId" => $roomId
    ];
    $options = [
        "http" => [
            "method" => "POST",
            "header" => "Content-type: application/json",
            "content" => json_encode($data)
        ]
    ];
    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    $result = json_decode($response, true);
    return $result;
}

// Get data
getAccessToken($appid, $appsecret);
$roomId = "your_roomid"; // Replace with actual room ID
$liveGoods = getLiveGoods($roomId);

// Display product information
foreach ($liveGoods["goods_info"] as $goods) {
    echo "Product ID: {$goods["goods_id"]}\n";
    echo "Product Title: {$goods["name"]}\n";
    echo "Cover Image: {$goods["cover_img"]}\n";
    echo "Price: {$goods["price"]}\n";
}

This script sends a POST request to the getgoodslist endpoint with the room ID and displays each product's ID, title, cover image, and price.

4. Conclusion

With the code examples above, you've learned how to implement core features of WeChat Mini Program live shopping using PHP. You can build on this foundation to add more features and integrate front-end components for a better user experience. As live commerce continues to rise, this functionality provides significant value for e-commerce platforms. Hopefully, this guide helps accelerate your project development.