1

I lifted some code off of StackOverflow to test some very basic PHP / Python integration, as I would like to eventually create a web interface for a Python machine learning platform I've been testing for a while now.

The code is as follows below:

PHP:

<?php

    $data = array('1', '2', '3');
    $result = shell_exec('python "C:\Program Files (x86)\Zend\Apache24\htdocs\Machine-Learning-Python-Integration\phptransfertest.py" ' . escapeshellarg(json_encode($data)));

    $resultData = json_decode($result, true);

    var_dump($resultData);

?>

Python:

import sys
import json

try:
    data = json.loads(sys.argv[1])

except:
    print "ERROR"
    sys.exit(1)

result = {'status': 'Yes!'}

print json.dumps(result)

But when I load this up in a browser, it only outputs NULL. I am unsure how to properly pass variables and files between Python and PHP, and most guides I have found tend to give me a NULL error.

2
  • If you follow guides for 100% and they give you a NULL error, perhaps your php is not set up correctly. If you did not follow them, but applied them to your problem, look at this: php.net/manual/en/function.shell-exec.php shell_exec is supposed to return NULL if something goes wrong with executing the command. What kind of string does . escapeshellarg(json_encode($data)) create? Does this info help: stackoverflow.com/questions/12197815/… Commented Aug 7, 2016 at 20:17
  • How did you solve this ? I'm coming across the same issue and have tried everything by now. Commented Feb 7, 2020 at 21:57

0

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.