Is it safe to use optoisolator for a longer period?
The circuitdiagram has some very basic problems:

left side:
There is no series resistor for the LED to limit the current.

right side:
I don't see an power source. 
And if there were any: the max. voltage between C and E of the PC817 is allowed to be 35V. Don't be tempted to switch 230V with the transistor of the optoisolator. You must use something like a triac or thyristor to do the high voltage switching.