Execute Python Script From PHP Script - php

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";
?>

Related

php call variable and put back on while loop

sorry for my last question where i try put some live code with ob_start buffer content is not helping me to solve my problem because buffer content just collects output text, it doesn't execute any code. thanks #akrys for your advices
what i want is to put code into while looping like this
$sql = $conn->query("SELECT * FROM `users`");
$var = $row['full_name'];
include('test.php');
after i call test.php contain while code like:
while($row = $sql->fetch_array()) {
echo $var;
}
everything is work if i replace $var with $row['full_name'];
but i get the name of row field from some script on index.php so i should access that file first then i call portable file contain query to fetch_array on test.php
how to make it work when i put it back with $var contain variable field name
thank you very much for your attention guys
you should to include before your code
page
test.php
<?php
$someVariable = 'hello'; // the variable only can access in here
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable $someVariable";
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
of course you can use define too
page test.php
<?php
define( "SOMEVARIABLE", hello );
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable ".SOMEVARIABLE;
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
you can use:
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
for more help, use the link below:
enter link description here

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>' );
?>

Problems while invoking swi-prolog from 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.

how to store output html to an var in php?

how to store output html to an var in php ?
how to set $a = require(xx.php)?
the xx.php outputs html , i want to store this html to a var , how to do?
please see my codes bellow:
a.php codes:
<?php
// a.php
echo 'hello world';
?>
b.php
<pre>
<?php
ob_start();
$a=require('a.php');
ob_clean();
echo $a; // get '1' but not 'hello world'
// can not post , why?
// can not post , why?
// can not post , why?
// can not post , why?
// can not post , why?
// can not post , why?
?>
You're after ob_get_clean():
$a = ob_get_clean();
The result of the "require" is not what you want. Try:
ob_start();
require('a.php');
$a = ob_get_clean();
echo $a;
You could also use shell_exec
<?php
$out = shell_exec("php -s $File");
?>
see: http://php.net/shell_exec
Hi according to your case you want output of any PHP page which you want to use in your current page in that case you need to use CURL in PHP
$ch = curl_init('a.php');
$htmlResponse = curl_exec($ch);
curl_close($ch);
echo $htmlResponse ;
die;
finally, I got the answer.
<?php
ob_start();
require('a.php');
$a=ob_get_clean();
echo $a; // get '1' but not 'hello world'
?>

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