Unfortunately code will take as long as it takes. Given that you've only shown a few lines of curl code and comments in between rather than pasting the entire affected code there isn't anything that any of us can suggest really other than not closing the curl handle between them.
Give that a go, if it works thats your problem. If it doesn't you'll need to post your code AS-IS without showing us your 'selected' lines.
My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!