0

I want to execute a python script from php script which will send a curl request to a web api and will return the output of the curl request to php script which will print it or do further processing.The problem is when I run the python script independently it produces correct output but when it is called from php script it doesn't work

I have tried changing permissions for the htdocs folder on xampp server and since I thought the image file which is being uploaded to server via api request is not accessible.

This is the part of php script which executes the python script

 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    sleep(5);
    $command = escapeshellcmd('python3 /opt/lampp/htdocs/script.py');
    $output = shell_exec($command);
    echo $output;
}

This is the python script

#!/usr/bin/env python
import sys
import subprocess
import os
import ast
import requests
from time import sleep
import datetime
file = open('/opt/lampp/htdocs/count.txt','r')
uid=file.read().strip()
file.close()
print(uid)
pipe = subprocess.Popen('curl -X POST "https://api- 
us.faceplusplus.com/facepp/v3/detect" -F 
"api_key=some_key" \
-F "api_secret=some_key" \
-F "image_file=@/opt/lamppp/htdocs/image.jpg"', stdout=subprocess.PIPE, shell=True, 
universal_newlines=True)
j = pipe.stdout.readlines()
print(j)

Expected output should print something like this when php page is called.

['{"image_id": "mprd5Pjd9ni+sPD4fWzv9g==", "request_id": "1554044606,8bba1d32-9b73-4457-a024-cb3ad66ee349", "time_used": 623, "faces": [{"face_rectangle": {"width": 134, "top": 159, "left": 242, "height": 134}, "face_token": "a0bc6e53782d0c38a837187a832da542"}]}'

but it prints empty sqaure braces.[]

2
  • 1
    In my language we like to say "from behind through the chest into the eye" if some overcomplicates things. Commented Mar 31, 2019 at 15:45
  • Why don't you do the curl call within php? Commented Mar 31, 2019 at 15:46

2 Answers 2

1

If you just want to make a CURL request you would be better off just using PHP's builtin CURL functions to keep your code less complex (https://www.php.net/manual/en/book.curl.php)

But if you still want to use the python script, then things to check are

  • The script is executable by the XAMPP user
  • You have the python SHEBANG on the first line of the python script #!/usr/bin/env python (see explanation)
  • Python is apart of the XAMPP users PATH (or use the full path instead of python in shell_exec)

Other than that, you can try using exec instead of shell_exec since shell_exec wont return a value if an error is thrown

$command = 'python /scripts/some_script.py';
$response = [];
$code = 0;
exec($command, $response, $code);
var_dump($code);
print_r($response);
Sign up to request clarification or add additional context in comments.

Comments

0

Make a shell file to execute your python script and make sure it's executable , Then make PHP to execute this shell file .

It Works For Me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.