get value of a variable from a python script
Question: I have a python script that returns a json object. Say, for example i run the
following:

    
    
    exec('python /var/www/abc/abc.py');
    

and it returns a json object, how can i assign the `json` object as a variable
in a php script.

### Example python script:

    
    
    #!/usr/bin/python
    import sys 
    
    def main():
        data = {"Fail": 35}
        sys.stdout.write(str(data))
    
    main()
    

### Example PHP script:

    
    
    <?php
    
    exec("python /home/amyth/Projects/test/variable.py", $output, $v);
    echo($output);
    
    ?>
    

The above returns an empty Array. Why so ?

I want to call the above script from php using the `exec` method and want to
use the json object returned by the python script. How can i achieve this ?

Update:

The above works if i use another shell command, For Example:

    
    
    <?php
    
    exec("ls", $output, $v);
    echo($output);
    
    ?>
    

Anyone knows the reason ?


Answer: If the idea is you'll have a Python script which prints JSON data to standard
out, then you're probably looking for [`popen`](http://php.net/popen).

Something like...

    
    
    <?php
    
    $f = popen('python /var/www/abc/abc.py', 'r');
    if ($f === false)
    {
       echo "popen failed";
       exit 1;
    }
    $json = fgets($f);
    fclose($f);
    

...will grab the output into the `$json` variable.

As for your example Python script, if the idea is you're trying to convert the
Python dictionary `{"tests": "35"}` to JSON, and print to standard out, you
need to change `loads` to `dumps` and `return` to `print`, so it looks like...

    
    
    import simplejson
    
    def main():
        data = simplejson.dumps({"tests": "35"})
        print data
    
    main()
    

