instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?
Yes, although the function names might not be what you expect.
Since the Python and Ruby answers are already posted, here is an PHP example:
$ip = gethostbyname('www.example.com');
$hostname = gethostbyaddr('127.0.0.1');