Had the same problem
Let me show you what I did
PHP :
base64_encode(json_encode($bodyData))
then
json_decode(shell_exec('python ' . base64_encode(json_encode($bodyData)) );
and in Python I have
import base64
and
content = json.loads(base64.b64decode(sys.argv[1]))
as Em L already mentioned :)
It works for me Cheers!
Answer from bobkoo on Stack OverflowVideos
Had the same problem
Let me show you what I did
PHP :
base64_encode(json_encode($bodyData))
then
json_decode(shell_exec('python ' . base64_encode(json_encode($bodyData)) );
and in Python I have
import base64
and
content = json.loads(base64.b64decode(sys.argv[1]))
as Em L already mentioned :)
It works for me Cheers!
You can base64 foods to string, then passed to the data to Python and decode it.For example:
import sys, base64
if len(sys.argv) > 1:
data = base64.b64decode(sys.argv[1])
foods = data.split(',')
print(foods)
Copy $jsonArr = array();
foreach($periodi as $key => $val){
$jsonArr[$key]['name'] = $val;
$jsonArr[$key]['data'] = $key; //there is no source for priority
//in your array so i just put $key as priority variable
}
json_encode($jsonArr);
You should make your array like this ...
CopyArray
(
[0] => Array
(
['name'] => 'Tokyo'
['data'] => 3
)
[1] => Array
(
['name'] => 'NewYork'
['data'] => 2
)
)
How to make the array ?
Copy$final_array = array();
foreach($data as $key=>$value)
{
$final_array[]=array(
"name"=>$key,
"data"=>$value
);
}
Then you just need to json_encode the $final_array.
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
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.
If you pass the JSON in your post to json_decode, it will fail. Valid JSON strings have quoted keys:
json_decode('{foo:"bar"}'); // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}'); // returns an object, not an array.
Try this:
$data = json_decode($your_json_string, TRUE);
the second parameter will make decoded json string into an associative arrays.
You creating invalid json by adding apiBlockTicketRequest to output
$json='apiBlockTicketRequest'.$data;
instead you can do
$json = json_encode(['apiBlockTicketRequest' => $prebook]);
Your code is correct to make that string but the entire output string ($json) will never parse as json, only the $data you encoded.