This function has a known bug (https://bugs.php.net/bug.php?id=58573) under some Linux installations: you can't encrypt your private key if you've compiled libssh2 using libgcrypt. According to the bug page, the solution is to rebuild libssh2 with OpenSSL. (Or don't encrypt your private key, but that seems irresponsible somehow.)
This isn't a bug report -- the bug is apparently already being fixed -- but a note for others, because it took me at least an hour of Googling to realize the problem wasn't with my own code.