Michael Scarpa
Michael Scarpa

Reputation: 121

passing data from php to python in laravel 5

So i have seen other questions like this, but what i have seen isn't working and i'm not sure if its a laravel 5 thing or not.

i have a python file - jobs.py:

import yaml
import requests
import os
import inspect
import sys
import datetime
import random
import urllib2
import cPickle 



print "hey"
print sys.argv[1]
print str(sys.argv)

In the normal linux command line if I type in the following:

python /home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py arg1 arg2

I will get this response as expected:

hey arg1 ['/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py', 'arg1', 'arg2']

But when i try to do the same thing in my php file:

public function getJobs()
{
    $jobs = \App\Job::all();
    $var1 = "arg1";
    $command = exec('python /home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py  $var1');
    return view('jobs/jobs_table', compact('jobs'));
}

And start using xdebug to see what is going on it says that its connected when its steps over $command but $command only returns "hey". I am trying to pass $var1='arg1' to the python script.

Thanks

Upvotes: 0

Views: 2239

Answers (1)

Don't Panic
Don't Panic

Reputation: 41810

Python is not receiving the contents of $var1, because when using a string with single quotes, as in

$command = exec('python /home/.../src/lib/jobs.py  $var1');

any contained variables will not be parsed. You must use double quotes like this:

$command = exec("python /home/.../src/lib/jobs.py  $var1");

You can read about variable parsing here.

Upvotes: 1

Related Questions