我试图让这段代码工作,但它不会。请有人帮助我。这是第一个文件 tts.php:
<?php
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?tl=en&q={$text}");
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?tl=en&q={$text}");
return $mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
第二个文件,index.php:
<?php
require "tts.php";
$tts = new TextToSpeech();
$tts->setText("Hello World!");
$tts->saveToFile("voice.mp3");
?>
这是错误:
*我在本地主机上运行代码