With the use of following snippet below, you will be able to use Gravatars in your script.

Snippet Source/Credit: apphp.com