Make a WeChat Voice On-Demand System and a voice on-Demand System

Source: Internet
Author: User
Tags php define

Make a voice-on-Demand System and a voice-on-Demand System

Recently, I was working on a project to access the public platform. Through the public platform, you can easily build a fully functional mobile application. Yesterday we found that developers can use mobile phone numbers to apply for an interface to test accounts and experience advanced interfaces.

The application of this article will use speech recognition and music reply interfaces in advanced interfaces. Through these two interfaces, you can experience the charm of music.

This application is simple: you report a song name to the public platform, and the public platform returns this song to you. This article will teach you how to implement this seemingly complex mobile application step by step.

The following is the QR code of the test account I applied for. You can check the test results. However, the test account is only valid for one year and can have up to 20 testers.

The other is my personal subscription number, which has more features and has no limit on the number of people, but cannot recognize speech. If you have said so much, you should do it yourself.


First, apply for the test account and Baidu's bae cloud platform:

Public platform test account see: http://mp.weixin.qq.com/debug/cgi-bin/sandbox? T = sandbox/login

See Baidu bae: http://developer.baidu.com/bae/

Related Configuration:

After the application is completed, perform the following configurations:

1. Create an application on bae and apply for a domain name

2. Download the php DEMO code from the public platform and upload it to the bae.

3. Adjust the php code and configure the interface

Then prepare the music api:

I found a private music api of Baidu on the Internet. For details, see:

Http://blog.csdn.net/leixiaohua1020/article/details/12927707

Start encoding:

1. First, let's take a look at the speech recognition interfaces used.

 

 

We send the voice to the server, and the server converts the voice into text. After obtaining the text, we call the music api to obtain the music, and then return the music to the client through the reply music message in the passive reply.

The Code is as follows: (this code can be directly uploaded to your Baidu bae platform. You only need to modify the token)

<? Php define ("TOKEN", "yourtoken"); $ wechatObj = new wechatCallbackapiTest (); // $ wechatObj-> valid (); $ wechatObj-> responseMsg (); class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature ()) {echo $ echoStr; exit ;}} public function responseMsg () {// get post data $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // parse post data if (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ type = $ postObj-> MsgType; switch ($ type) {case "voice"; $ resultStr = $ this-> getMusicContent ($ postObj); echo $ resultStr; exit; break ;}} private function checkSignature () {$ signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ Timestamp, $ nonce); sort ($ tmpArr); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr ); if ($ tmpStr ==$ signature) {return true;} else {return false;} public function getMusicContent ($ postObj) {$ ret = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [music]> </MsgType> <Music> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA []> </Description> <MusicUrl> <! [CDATA [% s]> </MusicUrl> <HQMusicUrl> <! [CDATA [% s]> </HQMusicUrl> <FuncFlag> <! [CDATA [1]> </FuncFlag> </Music> </xml> "; $ recognition = $ postObj-> Recognition; $ keywordc = urlencode ($ recognition ); $ musicapi = "http://box.zhangmen.baidu.com/x? Op = 12 & count = 1 & title = {$ keywordc }\\ \ $ "; $ simstr = file_get_contents ($ musicapi); $ musicobj = simplexml_load_string ($ simstr ); $ I = 0; $ musicurl; foreach ($ musicobj-> url as $ itemobj) {$ encode = $ itemobj-> encode; // process decode, we found that there was a problem when processing music, so here we delete a parameter $ decode = $ itemobj-> decode; $ removedecode = end (explode ('&', $ decode )); if ($ removedecode <> "") {$ removedecode = "&". $ removedecode;} $ decode = str_replace ($ removede Code, "", $ decode); $ musicurl = str_replace (end (explode ('/', $ encode), $ decode, $ encode); break ;} $ resultStr = sprintf ($ ret, $ postObj-> FromUserName, $ postObj-> ToUserName, time (), $ recognition, $ musicurl, $ musicurl ); return $ resultStr ;}}?>

 

Summary: Speech Recognition is very powerful. My Mandarin is not a standard, but it can be recognized every time. In addition, Baidu music api has everything to offer.

From the above description, we can see that I simply configured and wrote a few simple php code to implement a complex speech karaoke system.

 

Experience:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.