0

我试图让这段代码工作,但它不会。请有人帮助我。这是第一个文件 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");
?>

这是错误:错误

*我在本地主机上运行代码

4

4 回答 4

2

return $mp3data;应该return $this->mp3data;

于 2012-12-08T21:34:16.347 回答
1
return $this->mp3data;

代替

return $mp3data;
于 2012-12-08T21:34:29.587 回答
1

第 27 行大概是setText()函数中的这一行...

return $mp3data;

它应该是

return $this->mp3data;
于 2012-12-08T21:34:53.837 回答
0

你应该改变行

return $mp3data;

return $this->mp3data;
于 2012-12-08T21:34:41.663 回答