卷曲打破脚本

I have such a weird problem with CURL in PHP. The thing is, i'm communicating with local database through API which returns JSON. I'm using CURL to do it and the exact way I do it, is that first all the categories are fetched then, within foreach, CURL is called again for each of the categories to fetch the products in it. We were moving the website to new server and the thing stopped working all of a sudden and I can't tell why.

So, everything works fine and the products are being fetched correctly until the 4th category (which is larger I'm assuming). After the fourth category the script stops running and it doesn't output anything to the screen.

I've tried numerous things, changing memory_limit in php.ini prolonging time limit and whatnot. The result is always the same and the output is this

enter image description here

After 04, Hello should be outputted as should all the other categories (atleast 20 of them)

Here's a piece of my CURL code within foreach function.

foreach ($categories as $key => $value) {
    echo $key.' ';
    $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
    $fp = fopen(dirname(__FILE__).'/response'.$key.'.txt', 'w');
    $url_get_products = $url.$key;
    $ch = curl_init($url_get_products);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_STDERR, $fp);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
    $html = curl_exec($ch);
    $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
    curl_close($ch);
    echo 'Hello<br>';

I've even managed to get the output to a file and it seems it's working properly when I output it to the file but the script still stops running.

Then, since I know which categories are the reason behind this problem I've even separated it outside the foreach loop which redirected me to 404 page on my server.

Anyone with the same problem?