This is my code so far
$dataraw = $_SESSION['image'];
$datagambar = json_encode($dataraw);
echo '<pre>';
print_r($dataraw);
echo '</pre>';
print($escaped_json);
$type1 = gettype($dataraw);
print($type1);
$type2 = gettype($datagambar);
print($type2);
This is $dataraw output, the type is array
Array
(
[0] => Array
(
[FileName] => 20221227_202035.jpg
[Model] => SM-A528B
[Longitude] => 106.904251
[Latitude] => -6.167665
)
[1] => Array
(
[FileName] => 20221227_202157.jpg
[Model] => SM-A528B
[Longitude] => 106.9042428
[Latitude] => -6.1676580997222
)
)
This is $datagambar output, the type is string
[{"FileName":"20221227_202035.jpg","Model":"SM-A528B","Longitude":106.904251,"Latitude":-6.167665},{"FileName":"20221227_202157.jpg","Model":"SM-A528B","Longitude":106.9042428,"Latitude":-6.167658099722223}]
Pass to python
echo shell_exec("D:Anacondapython.exe D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py $datagambar");
This is my python test.py
import sys, json
import os
import pymysql
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplcursors as mpl
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score,hamming_loss,classification_report
json_list = []
escaped_json1 = sys.argv[1]
# this is working but its only a string of array json
# print(escaped_json1)
# this is working but its only a string of array json
json_list.append(json.loads(escaped_json1))
parsed_data = json.loads(escaped_json1)
print(json_list)
print(parsed_data)
When i do print(escaped_json1) it display a string of array json from php($datagambar).
python output:
Hello world has been called [{"FileName":"20221227_202035.jpg","Model":"SM-A528B","Longitude":106.904251,"Latitude":-6.167665},{"FileName":"20221227_202157.jpg","Model":"SM-A528B","Longitude":106.9042428,"Latitude":-6.167658099722223}]
I use apache as my server with phpmyadmin and anaconda.
T tried using print(type(escapedjson1))
or print(type(escapedjson1)) but it doesn’t display the type
json.loads didn’t change the type of data to python array
How to loads it and make the string array into a variable array so i can call it and convert it to dataframe?.
2
Answers
You just need to enclose in single quotes the argument to python:
shell_exec("python3 test.py '$json'");
Example
file.php
test.py
Output
See:
Escaping double qoutes when sending JSON as argument in python program
Update: A Completely different approach
There is a difficulty with the PHP script JSON-encoding a structure to produce a JSON string and then passing it as a command line argument since the string needs to be placed in double quotes because there can be embedded spaces in the encoded string. But the string itself can contain double quotes as start of string characters and within such a string. Confused? Who wouldn’t be?
There is no problem however with writing such a string to a file and having the Python program read it in and decode it. But we don’t want to have to deal with temporary files. So the solution is to pipe the data to the Python program where it can then be read in as stdin
Let’s assume your array looks like:
Note that I have modified your example slightly so that the first
FileName
field contains a"
character, a'
character, an escape sequencen
representing the newline character and finally some spaces. Although your example does not contain these characters or some other escape sequence, I would like to be able to handle such a condition should it arise. This solution should work with such input.PHP File
test.py
Prints: