How to call out names automatically? - php

I wanted to implement an automatic calling machine using Raspberry Pi 3, which pronounces the things I send to it via a web form.
I turned my Raspberry into a web server using Apache2 and had the following code in its /var/www/html. I am using google_speech 1.0.15 TTS.
PHP
<?php
if($_POST){
$name=$_POST['name']
shell_exec("/var/www/html/call.sh $name")
}
<html>
<body>
<form action="" method="post">
Name<input type="text" value="name">
<input type="submit" name="submit">
</form>
<\body>
<\html>
call.sh
#!/bin/bash
google_speech -l en "Hello $1"
echo "call.sh executed"
I have my audio system connected to the Pi. The script gets executed, as the last echo statement get printed but the speech output is not coming out of the Pi. I believe the line google_speech -l en "Hello $1" is not getting executed. The script works fine when executed over terminal. What should I do? Is there any better way of implementing the same?

Your tag doesn't have a "name" defined, so it's not being sent:
It is:
Name<input type="text" value="name">
And should be:
Name <input type="text" name="name">
Your code has some glaring security problems, but those lay beyond the scope of your question. I assume you are going to be the sole user for that application hosted on your Pi.

Related

How to take real time input output from a python script on PHP

I want to take input from user through a html form, process it through a python script and print the output on html page. The problem is the whole python script is executed each time, while I want the script to give real time output for each input. How can I manage to do this ?
Here is what I am doing so far.
<?php
$vout='';
if(isset($_POST['submit']))
{
$vin=$_POST['input_text'];
$vout=exec('python bot.py '.$vin);
}
?>
<form method="post">
<label>
BotIn: <input type="text" name="input_text">
</label>
<input type="submit" name="submit">
</form>
</br></br>
<label>
<p>Bot: <?=$vout?></p>
</label>
The best way to solve your problem will be running simple python app using, let's say, Flask and hit to specific endpoint you'll make in it. Everything then could take place through localhost, if you'll set it on the same machine.

How to execute a script in a php

I have an overhead controller (sort of a magic wand for some robots) plugged into my usb port.
I am trying to create a web server on linux that allows users to send a preloaded code (so no one is inputting anything) to the robots. I have embedded the command in a script file that I am trying to execute in a php file. The bit of code that executes the script file in php is run when a button is pressed on the web server. However, when I press the button nothing happens. The code doesn't get sent to the controller and no errors are given.
I can run the script in the command line with no problems. But when I try to execute it run it from the web server, nothing happens. Is there a way I can resolve this?
This is what's in the php file
<html>
<body>
<h1><font face="verdana" color="green">SwarmArena</font></h1>
<hr>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type ="text" name = "name"/>. ; <br>
Age: <input type ="text" name="age"/>. ; <br>
New Code: <textarea cols=40 rows=10 name="Newcode">Default text</textar$
<input type = "submit" name="enter" value="Enter" />
<input type = "submit" name="insert" value="insert" />
<input type = "submit" name="select" value="select" />
<input type = "submit" name="sleep" value="sleep" />
<input type = "submit" name="reset" value="reset" />
</form>
</body>
</html>
<?php
if(isset($_POST['sleep'])){
$out = shell_exec('./sleep.sh');
echo "$out";
}
?>
this is what's in the sleep.sh
#!/bin/sh
cd kilocmd && ./kcmd -s /dev/ttyUSB0 -c sleep
echo "done";
Thanks
I don't know how you are doing this. But onclick the button it should submit to a php page which will run the shell scripts using exec and shell_exec php commands .
More Over that Several Possibilities May Cause Issues like
- exec and shell_exec are disabled in php.ini
- The path to the executable is wrong. If the script is in the same
directory as the php file, try exec(dirname(__FILE__) .
'/myscript.sh');
Assuming you are executing the command correctly from the PHP script, it may be useful to look into permission issues.
Log into the user that the web-server is running under and try executing the command manually from there. I personally have my webserver running under a user named apache.
To view your current user run whoami in the command line.
To change user run su - newuserhere.
If this works, or you are running the web-server under root then you can most likely rule-out any permission issues.

Running bash script using PHP

I am working on little project. Task is get ip or address from input field and then run ping command for that ip and finally print result in the webpage.
I have written code:
<FORM ACTION="pingIp.php">
Enter Host: <INPUT name="host">
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
<div class="results"></div>
I don't know how to run ping command and retrieve results on webpage. Can you please give little php code as an example?

Executing two commands on the same page with exec()

I am using the following code to try and execute two different commands from button clicks in a php webpage.
<?php
if (isset($_POST['button'])) { exec('/usr/local/bin/node desktop/server.js'); } ?>
<form method="POST">
<p>
<input type="hidden" name="button" value="1">
<input type="submit" value="Start Video">
</p>
</form>
<?php
if (isset($_POST['button'])) { exec('/usr/local/bin/node desktop/test.js'); } ?>
<form method="POST">
<p>
<input type="hidden" name="button" value="1">
<input type="submit" value="Test">
</p>
</form>
I can get the first command to execute ok to start the server and it works fine but when I then click on the second one nothing happens. I have tried each of them just on the webpage individually and they both work fine on their own but what i want to do is start the server then run the other script, but it won't allow me for some reason?
Please note that nodejs applications normally don't stop. And that exec will wait for the end of the called program. The combination of both will freeze PHP until the nodejs process has finished. Depending on your web server, there can be more or fewer simultaneous PHP processes or threads, but normally this number is limited, so you should avoid this situation. And there is another reason to avoid this situation: Your browser will trying to load the page forever (until timeout) because it never receives the end of the page.
You can simply avoid this problem, if you append an ampersand to the end of the command line:
exec('/usr/local/bin/node desktop/server.js &');
This will make the nodejs progress starting in background, and your PHP script can continue.

PHP POST not working correctly

When I submit my form, I get sent to the correct file as specified in the action attribute of my form, but the PHP in the file isn't printing the variables at all... I've combed through posts of other people having the same issue, but none of their solutions fix my problem. I've stripped my code down to a simple, textbox, button, and a php file that's supposed to print the textbox value.
If it matters, I'm running this locally in chrome, not using any servers or websites yet, I'd like to get my code working locally before I upload to my server.
HTML
<html>
<form action="Submit.php" method="post">
<input type="text" placeholder="First Name" name="firstName" id="firstName" required>
</br></br>
<input type="submit" name="submitted" value="Submit">
</form>
</html>
PHP
<html>
<body>
Name <?php echo $_POST["firstName"]; ?><br>
</body>
</html>
All I get when I click the button is a white page with "Name" printed.
Thanks!
Running scripts in response to HTTP requests is something that's done by the webserver. If you just use local files, the script will simply be loaded into the browser as a text or HTML file, it won't be executed. You can't do form processing like this.
You need to run a local server, then access the form as http://localhost/form.html

Categories