From what I understand from your code, you need the URL of a book image to print in a template file.
Ideally this should be in a plugin, or at the very least in a separate class / function in your theme. Mixing presentation code (HTML) with logic isn't a good practice.
Regarding the number of requests to the API, unless you have a very small number of visitors, you should save data that you get from the API locally so you don't make multiple requests to get the same data.
Since you're starting with PHP you might want to look for similar plugins and study their code.