Current Location: Home> Latest Articles> Implement Push Statistics and Device Management Features in PHP with Baidu Push Extension

Implement Push Statistics and Device Management Features in PHP with Baidu Push Extension

M66 2025-06-29

Introduction

Baidu Push is a powerful push service provided by Baidu, supporting real-time push notifications and various types of messages to mobile applications. For developers, Baidu Push not only simplifies the integration of message pushing but also offers rich push statistics and device management features. This article will demonstrate how to use Baidu Push extension in PHP applications to implement push statistics and device management features, along with code examples to help developers understand the implementation process.

What is Baidu Push?

Baidu Push is a messaging push service provided by Baidu, allowing you to push notifications, transparent messages, and more types of messages to mobile devices. It helps developers achieve efficient real-time communication and information delivery.

Preparation

Before implementing push statistics and device management features, you'll need to complete the following preparations:

  • Register an account and create an application on Baidu Developer Platform to get your API key and secret key.
  • Install the Baidu Push extension: Use Composer to install the Baidu Push extension by running the following command:
composer require baidu-aip/php-sdk-push

Implementing Push Statistics

The push statistics feature helps developers monitor the sending status of messages, including the number of successful sends, failed sends, and the usage of the quota.

Here is an example of code to implement push statistics:

<?php
use BaiduBaiduPushClient;
$client = new Client($apiKey, $secretKey);
// Get overall push statistics
$response = $client->report->getReport();
if ($response->isSuccess()) {
    $reportList = $response->getResult();
    foreach ($reportList as $report) {
        echo "Success: " . $report['success'] . ", Fail: " . $report['fail'] . ", Quota Usage: " . $report['quota'];
    }
} else {
    echo "Failed to get push statistics: " . $response->getErrorMsg();
}

Implementing Device Management

The device management feature helps developers manage the list of devices, including adding, deleting, and querying devices. Through this feature, developers can push messages to specific devices or device groups.

Here is an example of code to implement device management:

<?php
use BaiduBaiduPushClient;
use BaiduBaiduPushRequestsCreateTagRequest;
use BaiduBaiduPushRequestsQueryTagDevicesRequest;
$client = new Client($apiKey, $secretKey);
// Create a tag
$tag = 'test';
$response = $client->tag->createTag(new CreateTagRequest($tag));
if ($response->isSuccess()) {
    echo "Tag creation successful";
} else {
    echo "Tag creation failed: " . $response->getErrorMsg();
}
// Query device list under the tag
$response = $client->tag->queryTagDevices(new QueryTagDevicesRequest($tag));
if ($response->isSuccess()) {
    $deviceList = $response->getResult();
    foreach ($deviceList as $device) {
        echo "Device ID: " . $device['channel_id'];
    }
} else {
    echo "Failed to query device list: " . $response->getErrorMsg();
}

Conclusion

By using Baidu Push extension, we can easily implement push statistics and device management features. Push statistics help us understand the status of message delivery, while device management allows us to manage the device list and push messages to them efficiently. We hope this article and the code examples will help developers successfully integrate Baidu Push services into PHP applications.