-1

I got text2speech working, but I want to use GET method how can I do it? I mean I want to convert text2speech by URL like this: http://localhost/txt2speech/v.php?textbox=Hello Word

If I enter that URL in the browser I expect it to play the audio.

I can successfully convert text into speech, however the problem is it plays the same file.

EXAMPLE

If I send http://localhost/txt2speech/v.php?textbox=HelloWord and after that if I send http://localhost/txt2speech/v.php?textbox=SecondString

It plays HelloWorld, but I expect it to play SecondString However if I try in a different window it will play the SecondString file.

I have another problem. It will not work if I pass a string with a space in it.

EXAMPLE

http://localhost/txt2speech/v.php?textbox=Hello This is sentence with spaces

These are the files I am using:

index.php

<html>
<body> 
<h2>Text to Speech PHP Script</h2>

<form action="v.php" method="GET">
Enter your text: <input name="textbox"></input>
</form>

</body>
</html>

v.php

<?php
 if($_GET){
     $text = substr($_GET['textbox'], 0, 100);
   $file  = 'filename';
 $file = "audio/" . $file . ".mp3";
  $mp3 = file_get_contents("https://translate.google.com.vn/translate_tts?ie=UTF-8&q=$sname+&tl=en&client=tw-ob");
 file_put_contents($file, $mp3);
}
?>

<?php  if($_GET){?>
<audio controls="controls" autoplay="autoplay">
  <source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
<?php }?>
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
xloss
  • 11
  • 6

1 Answers1

0

If your post code works, you can change post to get as seen below:

index.php

<html>
<body> 
<h2>Text to Speech PHP Script</h2>

<form action="v.php" method="get">
Enter your text: <input name="textbox"></input>
</form>


<?php  if($_GET){?>
<audio controls="controls" autoplay="autoplay">
  <source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
<?php }?>
</body>
</html>

v.php

<?php
 if($_GET){
     $text = substr($_GET['textbox'], 0, 100);
   $file  = 'filename';
 $file = "audio/" . $file . ".mp3";
 # Convert input to proper encoded url
 $sname = urlencode ( $sname );
  $mp3 = file_get_contents("https://translate.google.com.vn/translate_tts?ie=UTF-8&q=$sname+&tl=en&client=tw-ob");
 file_put_contents($file, $mp3);
}
?>

To prevent spaces from breaking your url, use urlencode function in php.

Anima-t3d
  • 3,431
  • 6
  • 38
  • 56
  • I know the difference between get and post, I already tried it, Thats why i posted here – xloss Apr 13 '19 at 00:33
  • Okay it works for me,, I added audio control into v.php anyway Thanks for quick responce <3 – xloss Apr 13 '19 at 00:40
  • But the problem is when i send strings with space it returns error , – xloss Apr 13 '19 at 00:42
  • What is the value you get? `%20`? You can use a function to clean input before parsing: https://stackoverflow.com/a/2109339/3163075 – Anima-t3d Apr 13 '19 at 00:45
  • I got nothing it says error: **Warning: file_get_contents(https://translate.google.com.vn/translate_tts?ie=UTF-8&q=hello fgfg+&tl=en&client=tw-ob): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\txt2speech\v.php on line 16** and the line no. 16 is : **$mp3 = file_get_contents("https://translate.google.com.vn/translate_tts?ie=UTF-8&q=$sname+&tl=en&client=tw-ob");** – xloss Apr 13 '19 at 00:52
  • To prevent spaces from breaking your url, use [urlencode](https://www.php.net/manual/en/function.urlencode.php) function in php. I updated my answer. – Anima-t3d Apr 13 '19 at 01:00
  • If you want to submit data to the server and update your client html based on the response you should use something like [AJAX request](https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form). When the data returns use something like jquery to update the source of the audio input. – Anima-t3d Apr 14 '19 at 09:19