I agree that this is PHP rather than WP question.
To bring it closer to WP I do suggest to take a look at following functionality:
- HTTP API is highly suggested for making HTTP requests;
add_post_meta()
and related meta functions to store post-specific information;the_shortlink()
function, that is suggested template tag to use for natvie short link functionality (and hook into for third party).