Found it! It was the following line I had to change:
curl_setopt($context, CURLOPT_FILE, $fh);
If I remove it, the file is returned as a binary string if I do this: replace this
self::execCurlAndClose($context, $response_headers);
by this
$thefilebinarystring = self::execCurlAndClose($context, $response_headers);
Or if I want to write to a tmp file, do not remove the line I showed above, but replace $fh by the $temp file you create before with $temp = tmp file()
However, I haven't updated the rest of the code yet, but this is the core!
Thank for your great answer, myself! ;)