Home >Backend Development >PHP Tutorial >How to use PHP to implement voice messages in WeChat applet

How to use PHP to implement voice messages in WeChat applet

王林
王林Original
2023-06-01 21:31:342130browse

In WeChat mini programs, voice messages are a common function, and users can interact through voice. So how to implement voice messages in WeChat applet in PHP? Let’s introduce it in detail below.

1. Voice messages in the WeChat applet

In the WeChat applet, users can send and receive messages through voice. The specific usage method is as follows:

  1. Send a voice message

In the chat window of the mini program, the user can long press the "Press and hold to speak" button to start recording a voice message . Once the recording is complete, the user releases the button and the voice message can be sent.

  1. Receive voice messages

When the user receives a voice message, the mini program will automatically play the voice and display the duration of the voice message and the text recognition result (if any) .

2. Use PHP to implement voice messages in WeChat mini programs

In PHP, to implement voice messages in WeChat mini programs, the following steps are required:

  1. Get access_token

access_token is a necessary parameter for calling the WeChat interface. We need to obtain the access_token first. This can be achieved through the following code:

function getAccessToken(){
    $appid = ""; // 小程序的appid
    $secret = ""; // 小程序的secret
    $url = sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",$appid,$secret);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($output,true);
    $access_token = $json["access_token"];
    return $access_token;
}
  1. Upload voice material

Uploading voice material requires sending a POST request to the WeChat server and specifying Content-Type in the request header. multipart/form-data. The specific code is as follows:

function uploadVoice($access_token, $filePath){
    $url = sprintf("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s",$access_token,"voice");
    $postdata = array(
        "media" => new CURLFile(realpath($filePath))
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    $output = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($output,true);
    $media_id = $json["media_id"];
    return $media_id;
}

Among them, $filePath represents the path of the voice file to be uploaded, and $media_id represents the media id returned after the upload is successful.

  1. Send a voice message

Sending a voice message requires sending a POST request to the WeChat server and specifying the message content in xml format in the request body. The specific code is as follows:

function sendVoiceMessage($access_token, $openId, $mediaId){
    $url = sprintf("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", $access_token);
    $xml = sprintf('<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[voice]]></MsgType><Voice><MediaId><![CDATA[%s]]></MediaId></Voice></xml>',
            $openId, "", time(), $mediaId);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

Among them, $openId represents the openid of the user who receives the voice message.

  1. Summary

The above are the steps to use PHP to implement voice messages in the WeChat applet. You can refer to the above code for specific operations and modify it according to the actual situation.

The above is the detailed content of How to use PHP to implement voice messages in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn