The incomplete headers message is generally an indicator that your script produced some output, which Apache sent to the browser. Be sure to capture stdout/stderr of any programs you execute.
You can see this if you run your script on the command line. If any output is produced before the script outputs the "Content-type: text/html\n\n" it will cause that error.