1

According to this mine question Python find value in array by filter

This is what I have done

data = {}

for result in results:
    if 'stackoverflow.com' in result['serp_url']:
        data['url'] = result['serp_url']
        data['rank'] = result['serp_rank']
        data['query'] = result['query']
        print(data)
        exit

This is PHP code

$test = shell_exec("python3 py.py");
var_dump($test);

And this is output

/home/user/Desktop/pyphp/index.php:4:string '{'url': 'https://stackoverflow.com/', 'rank': 1, 'query': 'stackoverflow'}
{'url': 'https://www.quantcast.com/stackoverflow.com', 'rank': 36, 'query': 'stackoverflow'}
' (length=168)

When I use json_decode($test) I'm getting null as output.

What's the best way to use as json or array output from Python in PHP?

Nicolas Marek
  • 41
  • 1
  • 6

4 Answers4

1

Thank you all for comments! According to that, I kind figure out solution.

Python script

data = {}
for result in results:
    if 'stackoverflow.com' in result['serp_url']:
        data['url'] = result['serp_url']
        data['rank'] = result['serp_rank']
        data['query'] = result['query']
        print(json.dumps(data))
        exit

PHP script

exec("python3 py.py", $output);

$test = [];

foreach($output as $key => $out) {
    $test[$key] = json_decode($out, true);
    print_r("Rank: " . $test[$key]['rank'] ." - ". $test[$key]['url']."<br>");
}

Output

Rank: 1 - https://stackoverflow.com/
Rank: 36 - https://www.quantcast.com/stackoverflow.com
Nicolas Marek
  • 41
  • 1
  • 6
0

You don't even have to even use Json package of Python. Here is little tweek. In Python and PHP code.

# Your py.py Looks Below: 
data = {}
data["aa"] = 100
# You can create any Valid Dictionary
print data

and Your Php File will have this code

<?php
    $test = shell_exec("python py.py");
    echo json_decode(json_encode($test));
    // $test itself is a json String you even dont need encode/decode
?>

And The Result in my Terminal got {'aa': 100} Which is Expected and you want.

Now Important Point is that shell_exec command will give you the string which is converted from Dictionary and fortunately, That string itself is a JSON, That why json_decode is returning NULL.

Above both Snippet works fine. Check them.

Shailendra2014
  • 789
  • 1
  • 7
  • 23
0

Python file:

import json
import base64

j_data = {
"name": "wick",
"age": 24,
}

jso_en = json.dumps(j_data , indent=4)
#encode with base64
res_arr_bytes = jso_en.encode("ascii")  
base64_bytes = base64.b64encode(res_arr_bytes)
base64_enc = base64_bytes.decode("ascii")
print(base64_enc)

PHP file:

$command = escapeshellcmd("python3 test.py");
$json_out = shell_exec($command);
$json_data = json_decode(base64_decode($json_out), true);
echo $json_data['name'];

output from php:

wick
Mirshad Rifas
  • 11
  • 1
  • 5
-1

The json you're generating is not valid (free objects outside of an array and strings between single quotes).

In your python code you should append your data objects to an array as you're creating them. Here's a rough attempt at solving your issue

import json

data = [] # use a list to keep your objects

for result in results:
    if 'stackoverflow.com' in result['serp_url']:
        record = {}
        record['url'] = result['serp_url']
        record['rank'] = result['serp_rank']
        record['query'] = result['query']
        data.append(record)
        # exit # <-- this doesn't seem correct, TBH
json.dumps(data)
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • In my opinion it's not a reason why it returns 2 objects at once... ofc objects won't aggregate, but still the problem is somewhere else. – Flash Thunder Sep 27 '18 at 10:54
  • @FlashThunder IMHO, the problem is that what OP posted is not what they're actually using to get that output (that's why I commented out the `exit`). I'll keep my answer here anyway just in case and I will understand and accept any downvote that comes my way :) – Federico klez Culloca Sep 27 '18 at 10:55
  • You may be right, that's why I only posted a comment. – Flash Thunder Sep 27 '18 at 10:57
  • Thanks for this, but didn't work, since I got null when executed script. – Nicolas Marek Sep 27 '18 at 11:06