Current Location: Home> Latest Articles> How to Fetch and Display Music Information in WeChat Official Account Using PHP

How to Fetch and Display Music Information in WeChat Official Account Using PHP

gitbox 2025-07-18

Music Information Feature in WeChat Official Account Development with PHP

WeChat Official Account development has gained great popularity recently, offering rich APIs and features that open up many possibilities for developers. Among them, fetching and displaying music information is a common requirement. This article explains how to implement this feature using PHP.

Preparation

First, you need a WeChat Official Account with developer mode enabled. Create a development project in the WeChat Official Platform and obtain your Developer ID (app_id) and Developer Secret (secret).

Next, install the PHP WeChat SDK to facilitate communication with WeChat servers. You can install it via Composer:

composer require overtrue/wechat

Once installed, you can begin coding.

Fetching Music Information

The WeChat platform provides APIs for retrieving music data. Using PHP's HTTP client, you can send requests and parse JSON responses.

use EasyWeChat\OfficialAccount\Application;

$config = [
    'app_id' => 'your-app-id',
    'secret' => 'your-app-secret',
    'response_type' => 'array',
];

$app = new Application($config);
$response = $app->http->get('https://api.weixin.qq.com/cgi-bin/media/get?id=xxxxxx');
$data = json_decode($response->getBody(), true);

$musicName = $data['name'];
$musicUrl = $data['url'];
$musicAuthor = $data['author'];

The code above creates a WeChat application instance, sets up necessary parameters, sends a GET request to the music API, retrieves the response, and extracts music name, URL, and author.

Displaying Music Information

After fetching music details, you can output HTML with PHP to display them in the Official Account interface:

echo "<h3>$musicName</h3>";
echo "<p>Author: $musicAuthor</p>";
echo "<audio src='$musicUrl' controls></audio>";

This uses PHP echo statements to output the music title, author, and an audio player for playback.

Conclusion

This article demonstrated how to use PHP for WeChat Official Account development to fetch and display music information, covering preparation, API interaction, and front-end rendering steps to help developers quickly integrate music features.

Combining PHP with the WeChat SDK offers powerful tools to simplify Official Account development and enable rich functionalities such as music playback. We hope this guide proves helpful for your development needs.