1

I have a little issue with a python script, the script getdata from some web socket, getting Json value, the result is send to a php script using os.system like that :

os.system('echo \' '+ result +' \' |  php sort.php')

but the result is echoed and not piped to the php script

for debug purpose the php script only do that :

$input = readfile("php://stdin");
$data = json_decode($input, true);
var_dump($data['input']);

any idea where the issue can come from ?

PS : result is a pure json string like this one :

{
    "error_code": 250,
    "user": {
        "id": "1",
        "login": "Talus",
        "password": "",
        "email": "",
        "display_name": ""
    },
    "session": {
        "id": "6",
        "user_owner": "1",
        "token": "3MJ9G53U3292DA4217ECCC201CE06D0B0AB0B5941580BA54",
        "last_activity": "2016-02-16 11:59:02",
        "in_game": "0"
    }
}
1
  • Perhaps it would be better to use subprocess ... (as the documentation for os.system suggests) Commented Feb 16, 2016 at 11:02

1 Answer 1

1

Try this:

import subprocess

proc = subprocess.Popen(['php', '/path/to/sort.php', result],
    stdout=subprocess.PIPE,
    shell=True,
    stdout=subprocess.PIPE)

script_response = proc.stdout.read()

Update

Here's an alternate solution..

Try this with your python script:

subprocess.call(['php', '-f', '/path/to/sort.php', result])

Then update your PHP script like this:

// Only execute via php-cli
if (php_sapi_name() == 'cli')
{
    if ($argc > 1)
    {
        // We use the index 1, because 0 == sort.php
        $result = json_decode($argv[1]);
        
        print_r($result);
    }
    else
    {
        exit('Invalid request @ '. basename(__FILE__) .':'. __LINE__);
    }
}
else
{
    exit('Invalid request @ '. basename(__FILE__) .':'. __LINE__);
}
Sign up to request clarification or add additional context in comments.

4 Comments

the json isn't echoed anymore, but the script seems to be paused, there s no output print by the php script any idea ? (I'm not a python dev I took back the work of a colleague, to continue hs project )
try changing your PHP script to load the json from argument, i.e. $input = isset($argv[0]) ? $argv[0] : '';
nothing change, even a simple echo "test"; in the php won't show up
this subprocess.call(['php', 'sort.php', result]) work like I want

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.