This shows how to control an ESP8226 over the internet. I haven't tried it and it will exposed your network to outside interference i think, but the steps will be pretty much te same for an ESP32
A safer way would be to post the data on some external website like mqtt or some other. You post and retrieve. That is much safer.