How to trigger lid close action programmatically?
There is no Apple supported programming interface to simulate the lid being closed.

You can put your Mac to sleep using this command line:

osascript -e 'tell application "System Events" to sleep'