Hello again
So this one's been driving me a little crazy recently, not least of all because i've never looked at php before. I'm doing an assignment where i'm creating a restapi service on gcp, which gets data from a rpi and displays the data on a mobile app. And it's working beautifully for simple sensor data like temp and humidity. However, i'm trying to send images too, and... Well, it aint fun.
I've uploaded test images to the server directly, and my Get function works. However, my Post doesn't. And i've no idea if it's caused by my php script on the server side, or my Python script sending the data. And because it's going through the cloud, i've no idea how to debug this. So anyone who can help with this is a life saver. My main question is: What format does 'data' send the image from the python code, to the php code? Here's the code:
Python
The PHP script
And in the database, there are 3 fields, id, time_stamp and image, with id being auto increment and time_stamp being current_timestamp. Image is a Blob.
I know this is a lot, but really the main focus for me is input['image'], because i don't have a clue if that's right, i'm not entirely sure which part of this is the data being read in (and this is after about 8 hours of googling). PHP seems to go over my head.
Thanks for any help people can give me,
Danny
So this one's been driving me a little crazy recently, not least of all because i've never looked at php before. I'm doing an assignment where i'm creating a restapi service on gcp, which gets data from a rpi and displays the data on a mobile app. And it's working beautifully for simple sensor data like temp and humidity. However, i'm trying to send images too, and... Well, it aint fun.
I've uploaded test images to the server directly, and my Get function works. However, my Post doesn't. And i've no idea if it's caused by my php script on the server side, or my Python script sending the data. And because it's going through the cloud, i've no idea how to debug this. So anyone who can help with this is a life saver. My main question is: What format does 'data' send the image from the python code, to the php code? Here's the code:
Python
Code:
def takePhoto():
threading.Timer(10.0, takePhoto).start()
now = datetime.datetime.now()
ret, frame = cap.read()
timestampStr = now.strftime("%d%b%Y(%H%M%S%f)")
timeStr = str(timestampStr)
path = os.path.dirname(os.path.abspath(__file__))
fullPath = path + '\\images\\frame' + timeStr + '.jpg'
cv2.imwrite(fullPath, frame)
with open(fullPath, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
payload = {"image": encoded_string}
imgdata = base64.b64decode(encoded_string)
filename = 'some_image.jpg'
with open(filename, 'wb') as f:
f.write(imgdata)
r = requests.post(url='http://mycloudipthatishouldn'tpastehere/index3.php/images/postimagedata', data={'image':encoded_string}, headers=headers)
The PHP script
Code:
// POST IMAGES
$app->post('/images/postimagedata', function ($request, $response) {
$input = $request->getParsedBody();
$input['image'] = json_encode($input['image']);
$sql = "INSERT INTO images (id,image) VALUES (:id,:image)";
$sth = $this->db->prepare($sql);
$sth->bindParam("id", $input['id']);
$sth->bindParam("image",$input['image']);
$sth->execute();
$input['id'] = $this->db->lastInsertId();
return $this->response->withJson($input);
});
And in the database, there are 3 fields, id, time_stamp and image, with id being auto increment and time_stamp being current_timestamp. Image is a Blob.
I know this is a lot, but really the main focus for me is input['image'], because i don't have a clue if that's right, i'm not entirely sure which part of this is the data being read in (and this is after about 8 hours of googling). PHP seems to go over my head.
Thanks for any help people can give me,
Danny
Last edited: