Problems while invoking swi-prolog from PHP - php

I'm trying to execute a swi-prolog file from PHP, but when i try to run this code, nothing is executed. This my code:
<HTML>
<HEAD>
<TITLE>Calling SWI-Prolog from PHP (short)</TITLE>
</HEAD>
<body>
<H1>Calling SWI-Prolog from PHP (short)</H1>
<?
$cmd = "nice -n15 /C:/Program Files/swipl/bin/swipl-win.exe -f test.pl -g test,halt";
?>
<P>
<PRE>
<?
system( $cmd );
echo "\n";
$output = exec( $cmd );
echo $output;
echo "\n";
exec( $cmd, $output );
print_r( $output );
echo "\n";
$output = shell_exec( $cmd );
echo $output;
echo "\n";
?>
</PRE>
</P>
</body>
</HTML>
When I run the php file from my server, it only shows the string Calling SWI-Prolog from PHP (short).

Activate error reporting and you probably will see some info showing what went wrong.
Add this at the beginning of the file:
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
?>
When done debugging you should remove the lines. You should never print error output in a productive environment.

Related

How execute Powershell script in PHP and output result?

Studying the interaction of Php with Powershell
Try the simplest script:
<?php
$query = shell_exec("powershell.exe -File E:\test.ps1");
echo $query;
?>
In the script test.ps1 - for example "Test-Connection Server"
Need to the answer in Powershell returned to the page Php, but in response to a white paper...
Please tell me some solution for this problem.. do Not have shell_exec. There may be other options?
You can store the output of powershell script in a variable and then echo it. Just change $psDIR to your PowerShell path (e.g. %SystemRoot%\system32\WindowsPowerShell\v2.0\)
<?php
$psPath = "powershell.exe";
$psDIR = "PathToPowrshell";
$psScript = "E:\test.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript;
$output= shell_exec($runCMD);
echo( '<pre>' );
echo( $output );
echo( '</pre>' );
?>

Execute Python Script From PHP Script

I have python file which is newtry.py and this is my code:
print ("hello world")
I also have php file which is importKeyword.php and this is my code:
<?php
$python = `python newtry.py`;
echo $python;
echo "yes";
?>
I want to print "hello world" from python in the browser but it only print "yes" which is from php file. I have look at this solution which is using backquote operator ( enter link description here ) and wondering why I can't make it.
You can use exec function
exec('python newtry.py', $output);
var_dump($output);
use 2>&1 to redirect the output
<?php
exec("python newtry.py 2>&1", $python);
print_r($python);
echo "yes";
?>

Displaying Raspberry pi output (coded in python 3) in php file

I am using Raspberry Pi, python and PHP. I can't get my count(output) into my php (I am using apache2). I code my raspberry pi in terminal (command prompt). I have tried using exec() and shell_exec(), however there is no output.
This are the code I have tried
exec("python /home/pi/testing.py");
$file = popen("/home/pi/testing.py","r");
echo $file
pclose($file);
<?php
#command = escapeshellcmd('/home/pi/testing.py');
$output = shell_exec($command);
echo $output;
?>
<!DOCTYPE html>
<html>
<head>
<style>
table,th,td {
border:1px solid black;
}
</style>
</head>
<body>
<?php
echo "<br>";
echo date ('y-m-d H:i:s');
$conn = new mysqli("localhost","root","password","menagerie");
if($conn->connect_error)
{
die("connection failed:" . $conn->connect_error);
}
$sql = "SELECT * FROM people";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Vistor</th><th>Entry</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["vistor"]. " </td><td> " . $row["Date"]. "
</td></tr>";
}
echo"</table>";
}
$conn->close();
?>
""<br>
""<br>
</body>
</html>
In Python file 'test.py' verify this text is in the first line: (see shebang explanation):
#!/usr/bin/env python
Also Python file should have correct privileges (execution for user www-data / apache if PHP script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.
chmod +x myscript.py

how to run python script from php on xamp?

Following is the php code that I was using. I am trying to run this script(residing in the same directory as the php file is in) and want to display the output of the script on webpage. Script is working fine through command prompt, but not working thru php script.
<html>
<head>
<title>py script</title>
</head>
<body>
<h1>hey there!</h1>
<?
$pyscript = 'C:\\xampp_new\\htdocs\\projectx\\USR.py';
$python = 'C:\\Python27\\python.exe';
exec("$python $pyscript ", $output, $return );
echo $return;
?>
</body>
</html>
<html>
<head>
<title>py script</title>
</head>
<body>
<h1>hey there!</h1>
<?
$pyscript = 'C:/xampp_new/htdocs/projectx/USR.py';
$python = 'C:/Python27/python.exe';
$command=escapeshellcmd('C:/xampp_new/htdocs/projects/USR.py');
$output=shell_exec($command);
echo $output;
?>
</body>
</html>
There are several options why your exec call won't work:
Are you running om safe_mode ? exec is disabled in safe mode
Your std output is at $output which is more interesting than the return value
if your python script working ? try in PHP: exec("$python $pyscript >test.txt"); and see if your text file has anything in it

Problem with ob_start function in php

I have this code, which uses ob_start php function. Which basically puts the echoed data into an html file. It works before. I do not know what version of php I was using then. But my current version is 5.3.0. I cannot explain why it wouldn't work. Because the script below is working and it just puts the output of that script into the html file:
<?php
ob_start();
?>
<h2>Customer Payment Summary</h2>
<img id="tablez" src="../img/system/icons/Oficina-PDF-icon.png"></img>
<?php
if($amtopay>=$curcred){
$custchange=$amtopay - $curcred;
$newcred = 0;
echo "Change: ". $custchange."<br/>";
query_database("DELETE FROM sales_transaction WHERE Cust_Name='$customer'", "onstor", $link);
}else{
query_database("UPDATE customer_credit SET CREDIT='$newcred' WHERE Cust_Name='$customer'", "onstor", $link);
echo "Remaining Balance: ". $newcred."<br/>";;
}
echo "Customer: ".$customer."<br/>";
echo "Amount paid: ". $amtopay. "<br/>";
echo "Date: ". $date." ". date('A');
close_connection($link);
?>
<?php
file_put_contents('../tmp/customerpay.html', ob_get_contents());
?>
Here's the output of the code above:
But when I checked the html file which I specified in the file_put_contents. It gives me this. And I don't really understand why:
My problem is how to get the correct output from the html file that is being produced.
You aren't closing your output buffer before you do a file_put_contents...
At the end of your script change it to the following:
//...
close_connection($link);
$contents = ob_get_contents();
ob_end_clean();
file_put_contents('../tmp/customerpay.html', $contents);
?>

Categories