Typically when I see this error it's when the found set is extremely large, and PHP is choking on processing the massive amout of XML returned. Since you're running a script that completes first before the XML is passed back it would make sense you'd see the script run ok.
So, I'd check the found set size upon completion of the script and see what that shows. If no data really needs to be returned as the script does all the work, omit all records at the end of the script for better performance.
Solved! Thank you Mark!