未定义的变量错误TTS PHP脚本

时间:2012-12-08 21:32:01

标签: php

我正在努力使这段代码工作,但事实并非如此。请有人帮帮我。这是第一个文件,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");
?>

这是错误:The error

*我在localhost上运行代码

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

return $this->mp3data;

而不是

return $mp3data;

答案 2 :(得分:1)

第27行可能是setText()函数中的这一行......

return $mp3data;

它应该是

return $this->mp3data;

答案 3 :(得分:0)

您应该更改行

return $mp3data;

return $this->mp3data;