0

したがって、.mp3ファイルに変換するために、引数を関数に設定する必要があります。この行を使用すると:$tts->setText($row['raspuns']);何も起こりませんが、私が書いた場合$tts->setText("Hello World!");、それは完全に機能します。これにより、ttsにテキストを取得させるための正しいコードを見つける必要があるという結論に達します。誰か助けてくれませんか?

<html>
    <head>
        <title>
            Bot
        </title>
        <link type="text/css" rel="stylesheet" href="main.css" />
    </head>
    <body>
        <form action="bot.php "method="post">
            <lable>You:<input type="text" name="intrebare"></lable>
            <input type="submit" name="introdu" value="Send">
        </form>
    </body>
</html>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("robo") or die(mysql_error());

$intrebare=$_POST['intrebare'];
$query = "SELECT * FROM dialog where intrebare = '$intrebare'"; 
$result = mysql_query($query) or die(mysql_error());
$row = $result;
?>

<?php
require "tts.php";
$tts = new TextToSpeech();
**$tts->setText($row['raspuns']);**
*//$tts->setText("Hello World!");*
$tts->saveToFile("voice.mp3");
$file='voice.mp3';
?>

<div id="history">
<?php       

    while (true == ($row = mysql_fetch_array($result))) {
    echo "<b>The robot says: </b><br />";
    echo $row['raspuns'];
    echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";
}
?>
</div>

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  $this->mp3data;
        } else { return false; }
    }

    function saveToFile($filename) {
        $filename = trim($filename);
        if(!empty($filename)) {
            return file_put_contents($filename,$this->mp3data);
        } else { return false; }
    }
}
?>
4

1 に答える 1

1

結果から行をフェッチする必要があります

ttsクラスの呼び出しがある場所でコードを機能させる場合は、次のように変更します。

$row = $result;

$row = mysql_fetch_row($result);

$row配列を再定義するコードブロックが下にあることに注意してください。

また、mysql_ *関数は非推奨であることに注意してください。代わりに、PDOまたはmysqli_関数を使用してください。あなたの現在のコードはSQLインジェクトに広く開かれています!

于 2012-12-08T23:46:15.520 に答える