PHP Call TTS TTS

Source: Internet
Author: User

Development background: Need machine to read the document, see BAT and News fly, the leader decided to use the message fly, actually bat is also very good
It's a lot of crap, just look at the code.

Header("content-type:text/html; Charset=utf-8 ");$url= ' Http://api.xfyun.cn/v1/service/v1/tts ';$appid= ' xxxxx ';$appkey= ' xxxxxxx ';$data=Array(    ' auf ' = ' audio/l16;rate=16000 ', ' aue ' = ' lame ', ' voice_name ' and ' XiaoYan ',);$time= (string) Time();$xparams=Base64_encode(Json_encode ($data));$checksum=MD5($appkey.$time.$xparams );$header=Array(    ' X-curtime: '.$time, ' X-param: '.$xparams, ' X-appid: '.$appid, ' X-checksum: '.$checksum, ' x-real-ip:127.0.0.1 ', ' content-type:application/x-www-form-urlencoded; Charset=utf-8 ',    );$content=Array(        The ' text ' = ' Chen Shou ' gave Cao the first evaluation called wit and cleverness, he was clever from childhood, but also very learned. Caocao is a great writer, even he created a literary age. The so-called Jian ' an character of the founder is three Cao, the first person to open a precedent is Caocao, and then his two sons. Then there is Jian ' an seven son, Cao is the wise man it is no controversy this evaluation is very accurate. It is controversial whether this wisdom is used in the right place, if there is no moral support, the more intelligent people the more harmful. Later in history to Chen Shou said this clever is he used the wrong place, sinister designs, this is the comments of posterity, but Chen Shou did not say so. The second evaluation is that he has the right to count when he was a child, and the original word is weighted. This weighting and use right is not a meaning, the right in ancient times is calculated meaning, the number in ancient times is not the meaning of the computation, but the meaning of the change. ‘);$response= Tocurl ($url,$header,$content);$header=$response[' Header '];$file= './audio/';if($header[' content_type '] = = ' Audio/mpeg '){    $filename.=$file.$time. '. mp3 '; $res=file_put_contents($filename,$response[' Body ']); Var_dump($res);}Else{    Echo $response[' Body '];} /** * Send data * @param String $url requested address * @param array $header custom Header data * @param array $content post data * @retu RN String*/functionTocurl ($url,$header,$content){    $ch=Curl_init (); if(substr($url, 0,5) = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer,false);//Skip Certificate Checkcurl_setopt ($ch, Curlopt_ssl_verifyhost,true);//Check that the SSL encryption algorithm exists from the certificate} curl_setopt ($ch, Curlopt_returntransfer,true); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_httpheader,$header); curl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields,Http_build_query($content)); $response= Curl_exec ($ch); $error=curl_error ($ch); //Var_dump ($error);    if($error){         die($error); }    $header= Curl_getinfo ($ch); Curl_close ($ch); $data=Array(' Header ' =$header, ' body ' =$response); return $data;}

The above is the finished code;

There's a couple of main points of attention here.

1. To add an IP whitelist to your own app, otherwise unresponsive

2. Error, multiple read error code and return hints, very helpful

3. If you don't understand, you can contact me.

PHP Call TTS TTS

Related Article

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.