[英]Undefined variable error TTS PHP script
我正在尝试使这个代码工作,但它不会。 请有人帮帮我。 这是第一个文件,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");
?>
这是错误:
*我在localhost上运行代码
return $mp3data;
应该return $this->mp3data;
return $this->mp3data;
代替
return $mp3data;
第27行可能是setText()
函数中的这一行......
return $mp3data;
它应该是
return $this->mp3data;
你应该改变行
return $mp3data;
至
return $this->mp3data;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.