How to set featured image from a variable

As far as I know, you can only run PHP from .php files, not .html, etc. So you should make sure you got that one right. But also you could try to use global $poster; for where you defined it and for where you are calling it. For example:
in widget.php global $poster; and in
output.php global $poster; echo $poster;