html not printing out output text - php

So i have two codes, one of them is just a simple code that i used to test out some php functions. I'm trying to print out the JSON string into my html, one of the codes is working but the other isn't.
So let's say that we have two sets of codes : first.php & first.py , second.php & second.py
first.php looks like this:
<?php
exec("C:/Users/hln/Anaconda3/envs/tensorflow1/python.exe C:/tensorflow1/models/research/object_detection/first.py", $output);
$someOutput = json_decode($output[0], true);
echo "<h3>" . $someOutput['rightCoordinate'] ."</h3>";
echo "<h3>" . $someOutput['leftCoordinate'] ."</h3>";?>
first.py looks like:
import json
a = 1 + 3
b = 5 + 5
x = {
"leftCoordinate": a,
"rightCoordinate": b
}
y = json.dumps(x)
print(y)
print()
second.php looks like this:
<?php
exec("C:/Users/hln/Anaconda3/envs/tensorflow1/python.exe C:/tensorflow1/models/research/object_detection/second.py C:/xampp/htdocs/w3layout/finalproject/uploads/10.PNG 10.PNG 2>&1",$output);
$someOutput = json_decode($output[0], true);
echo "<h3>" . $someOutput['theWidth'] ."</h3>"; ?>
second.py looks like:
outputvalues = {
"leftCoordinate" : x_min(each of these are already defined),
"rightCoordinate" : x_max,
"lowerCoordinate" : y_min,
"upperCoordinate" : y_max,
"numInjuries" : count,
"theWidth" : im_width,
"theHeight" : im_height
}
y = json.dumps(outputvalues)
print(y)
when i run them in command prompt, the first one will result:
{"leftCoordinate": 4, "rightCoordinate": 10}
and when i put it in my html it will print out 4 and 10
the second one have this result in command prompt:
{"leftCoordinate": 34.47790487855673, "rightCoordinate": 251.67991018295288, "lowerCoordinate": 208.6769086420536, "upperCoordinate": 388.4499931335449, "numInjuries": 1, "theWidth": 327, "theHeight": 503}
but it won't print out any result in html
is there anything that i should change?

This might not be a helpful answer because there does not seem to be anything wrong with your code.
I placed the following at the top of second.py:
import json
x_min = 34.47790487855673
x_max = 251.67991018295288
y_min = 208.6769086420536
y_max = 388.4499931335449
count = 1
im_width = 327
im_height = 503
and removed the absolute paths in second.php:
exec("second.py 10.PNG 10.PNG 2>&1",$output);
Now php second.php shows me <h3>327</h3>.
This is on Windows 10 with PHP 7.3.1 and Python 3.7.4.
Edit: Tried it on Ubuntu 19.04/PHP 7.2.19/Python 2.7.16 and got the same result.

Based on your comments the problem is the fact that the Python script generates a warning which is then passed to the PHP script.
One way to ignore the warnings is to add the -W option with a value ignore. The line in the PHP script would then look something like this:
exec("python -W ignore second.py", $output);

Related

Php and bash together to generate keys

I have this bash file which generates key and expire time :
#!/bin/bash
get_customer_url() {
local IP=${1:-127.0.0.1}
local SECRET=${2:-VERY_COOL_SECRET}
local EXPIRES="$(date -d "today + 30 minutes" +%s)";
local token="$(echo -n "${EXPIRES} VERY_COOL_SECRET" | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)"
echo "/${token}/${EXPIRES}/"
}
get_customer_url
and i call it via php like this
<?php $output = shell_exec('sh generate-keys-hls.sh'); echo "$output";?>
it works fine , it generates something like this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/
my problem is i want to put another php result in the some line like this
<?php $output = shell_exec('sh generate-keys-hls.sh'); echo "$output";?><?php echo get_post_meta($post->ID, 'file', true)?>
currently its printing the result in two lines i need it to be one just one line instead of this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/
file.mp4.m3u8
i want it to be like this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/file.mp4.m3u8
without white spaces or multiple lines !
Remove the whitespace around the result of shell_exec().
echo trim($output);

Does python json output values have maximum length for json_decode (php) to handle?

I am trying to write a php code that takes coefficients from a html form, sends them to a python algorithm that returns a json object. That object is a list of player names, basically {"Ji" : "Firstname Lastname"} for i from 1 to 15.
The python code (interface.py) I have to create this json is :
import json
i=0
for joueur in best_team:
i+=1
output["J%s"%(i)]=joueur['nom']
out=json.dumps(output)
print(out)
best_team is a list of player dictionnaries with data on them. My player names don't involve any non ASCII characters or whatever.
My php code is the following :
$command = "python interface.py";
$command .= " $coeff1 $coeff2 $coeff3 $coeff4 $coeff5 $coeff6 $coeff7 $coeff8 $coeff9 $coeff10 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
$data = fread($pid, 256);
$data= json_decode($data) ;
echo $data->J1;
flush();
ob_flush();
echo "<script>window.scrollTo(0,99999);</script>";
usleep(100000);
}
pclose($pid);
I call the coefficients from the html and then send back the results via a js file.
But I just get the following error : Notice: Trying to get property of non-object.
Nothing wrong with the js file because if I try instead :
$string = '{"foo": "bar", "cool": "attributlong"}';
$result = json_decode($string);
echo $result ->cool;
It works.
Also if I have instead in my python file :
out={"foo":"bar","word":"longerthaneightcharacters"}
out=json.dumps(out)
print(out)
It works as well (replacing J1 by word in php code of course).
And funny enough, if i have in python:
output={}
i=0
for joueur in best_team:
i+=1
output["J%s"%(i)]="short"
output["J%s"%(i)]=str(output["J%s"%(i)])
out=json.dumps(output)
print(out)
It works, and if I replace "short" by "longerthaneightcharacters" it doesn't work anymore.
So basically my question is, why is there a maximum number of characters in my output loop and how can I bypass it ? Thanks, I am very confused.

Calling Python script from PHP does not pan out

I am trying to extract major keywords from a text or sentence using python. I am using python's RAKE module. The following python code works well in the console. But, when I am trying to call the python script from PHP, the script does not parse any new text or sentence which I stored in a php variable, and instead outputs the old text/sentence even though I commented out it within the python script and replaces it with sys.argv[1] argument. In various ways, within PHP I tried to solve this problem using PHP's exec and passthru commands without any luck and so I finally decided to post my problem here.
PHP script
$var1 = 'The extra sleep will help your body wash out stress hormones.';
Technique(1)
$output = exec("python rake_tutorial.py .$var1");
Technique(2)
$output = exec('python rake_tutorial.py ' .$var1, $result);
Technique(3)
$command = 'python rake_tutorial.py ' . $var1;
$output = passthru($command);
Technique(4)
$output = exec("python rake_tutorial.py $var1", $result);
echo '<pre>' . print_r($result, true);
Here is my Python code
__author__ = 'a_medelyan'
import rake
import operator
import sys
# EXAMPLE ONE - SIMPLE
stoppath = "SmartStoplist.txt"
# EXAMPLE TWO - BEHIND THE SCENES (from https://github.com/aneesha/RAKE/rake.py)
# 1. initialize RAKE by providing a path to a stopwords file
rake_object = rake.Rake(stoppath)
# text = "What you use depends on your baby's age and physical development."
# 1. Split text into sentences
sentenceList = rake.split_sentences(text)
# generate candidate keywords
stopwordpattern = rake.build_stop_word_regex(stoppath)
phraseList = rake.generate_candidate_keywords(sentenceList, stopwordpattern)
print "Phrases:", phraseList
# calculate individual word scores
wordscores = rake.calculate_word_scores(phraseList)
# generate candidate keyword scores
keywordcandidates = rake.generate_candidate_keyword_scores(phraseList, wordscores)
for candidate in keywordcandidates.keys():
print "Candidate: ", candidate, ", score: ", keywordcandidates.get(candidate)
# sort candidates by score to determine top-scoring keywords
sortedKeywords = sorted(keywordcandidates.iteritems(), key=operator.itemgetter(1), reverse=True)
totalKeywords = len(sortedKeywords)
# for example, you could just take the top third as the final keywords
for keyword in sortedKeywords[0:(totalKeywords / 3)]:
print "Keyword: ", keyword[0], ", score: ", keyword[1]
print rake_object.run(sys.argv[1])
sys.stdout.flush()
# print rake_object.run(text)
Looks like your exec command is going wrong.Try the following command:
$var1 = 'The extra sleep will help your body wash out stress hormones.';
$output = exec("python rake.py '".$var1."'");
Hope this helps!
Just thought I'd mention that there is a RAKE implementation in php https://github.com/artofzen/RAKE-PHP

How to Correctly Run Python Script from PHP [duplicate]

This question already has answers here:
Running a Python script from PHP
(10 answers)
Closed 7 years ago.
I have a python script that I would like to run from PHP. This is my PHP script:
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
And this is my Python script:
import sys, json
# Load the data that PHP sent us
try:
data = json.loads(sys.argv[1])
except:
print "ERROR"
sys.exit(1)
# Generate some data to send to PHP
result = {'status': 'Yes!'}
# Send it to stdout (to PHP)
print json.dumps(result)
I would like to be able to exchange data between PHP and Python, but the above error gives the output:
ERROR NULL
Where am I going wrong ?
:::::EDIT::::::
I ran this:
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$temp = json_encode($data);
$result= shell_exec('C:\Python27\python.exe test.py ' . "'" . $temp . "'");
echo $result;
I am getting No JSON object could be decoded
On my machine, the code works perfectly fine and displays:
array(1) {
'status' =>
string(4) "Yes!"
}
On the other hand, you may make a few changes to diagnose the issue on your machine.
Check the default version of Python. You can do this by running python from the terminal. If you see something like:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
you're fine. If you see that you are running Python 3, this could be an issue, since your Python script is written for Python 2. So:
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[...]
should be a clue.
Again from the terminal, run python myScript.py "[\"as\",\"df\",\"gh\"]". What do you see?
{"status": "Yes!"}
is cool. A different response indicates that the issue is probably with your Python script.
Check permissions. How do you run your PHP script? Do you have access to /path/to/? What about /path/to/myScript.php?
Replace your PHP code by:
<?php
echo file_get_contents("/path/to/myScript.php");
?>
Do you get the actual contents?
Now let's add a few debugging helpers in your PHP code. Since I imagine that you are not using a debugger, the simplest way is to print debug statements. This is OK for 10-LOC scripts, but if you need to deal with larger applications, invest your time in learning how to use PHP debuggers and how do use logging.
Here's the result:
/path/to/demo.php
<?php
$data = array('as', 'df', 'gh');
$pythonScript = "/path/to/myScript.py";
$cmd = array("python", $pythonScript, escapeshellarg(json_encode($data)));
$cmdText = implode(' ', $cmd);
echo "Running command: " . $cmdText . "\n";
$result = shell_exec($cmdText);
echo "Got the following result:\n";
echo $result;
$resultData = json_decode($result, true);
echo "The result was transformed into:\n";
var_dump($resultData);
?>
/path/to/myScript.py
import sys, json
try:
data = json.loads(sys.argv[1])
print json.dumps({'status': 'Yes!'})
except Exception as e:
print str(e)
Now run the script:
cd /path/to
php -f demo.php
This is what I get:
Running command: python /path/to/myScript.py '["as","df","gh"]'
Got the following result:
{"status": "Yes!"}
The result was transformed into:
array(1) {
'status' =>
string(4) "Yes!"
}
yours should be different and contain a hint about what is happening.
I got it to work by adding quotes around the argument!
Like so:
<?php
$data = array('as', 'df', 'gh');
$temp = json_encode($data);
echo shell_exec('python myScript.py ' . "'" . $temp . "'");
?>

how can i assign values to my c program with php

I'm trying to run a C program of adding two numbers with PHP in a web browser. But when I run the command
exec"gcc name.c -o a & a" it
returns some garbage result like sum is : 8000542.00. It doesn't ask for any input.
I want to give inputs to scanf from the browser. Please suggest to me how can I resolve my problem.
I have tried this but couldn't handle it successfully.
$desc = array(0=> array ('pipe','w'), 1=> array ('pipe','r'));
$cmd = "C:\xampp\htdocs\add.exe";
$pipes=array();
$p = proc_open($cmd,$desc,$pipes);
if(is_resource($p))
{
echo stream_get_contents($pipes[0]);
fclose($pipes[0]);
$return_value=proc_close($p);
echo $return_value;

Categories