Handle JSON decode errors in API requests, throw synthetic error.

Moves the json_decode() error handling below where it is in xavier's version, so it applies for both cURL and file_get_contents().