title: cURL request in a loop sometimes returning nothing at all
body: <h3>The issue:</h3>
  
  <p>I'm working with PHP, cURL and a public API to fetch json strings.
  These json strings are formatted like this (simplified, average size is around 50-60 kB):</p>
  
  <pre><code>{
     "data": {},
     "previous": "url",
     "next": "url"
  }
  </code></pre>
  
  <p>What am trying to do is fetch all the json strings starting from the first one by checking for the "next" attribute. So I have a while loop and as long as there's a "next" attribute, I fetch the next URL.</p>
  
  <p><strong>The problem is sometimes, randomly the loop stops before the end and I cannot figure out why after many tests.</strong></p>
  
  <p>I say randomly because sometimes the loop goes through to the end and no problem occurs. Sometimes it crashes after N loops.</p>
  
  <p>And so far I couldn't extract any information to help me debug it.</p>
  
  <p>I'm using PHP 7.3.0 and launching my code from the CLI.</p>
  
  <h3>What I tried so far:</h3>
  
  <p><strong>Check the headers:</strong></p>
  
  <p>No headers are returned. Nothing at all.</p>
  
  <p><strong>Use curl_errno() and curl_error():</strong></p>
  
  <p>I tried the following code right after executing the request (curl_exec($ch)) but it never triggers.</p>
  
  <pre><code>if(curl_errno($ch)) {
     echo 'curl error ' . curl_error($ch) . PHP_EOL;
     echo 'response received from curl error :' . PHP_EOL;
     var_dump($response); // the json string I should get from the server.
  }
  </code></pre>
  
  <p><strong>Check if the response came back null:</strong></p>
  
  <pre><code>if(is_null($response))
  </code></pre>
  
  <p>or if my json string has an error:</p>
  
  <pre><code>if(!json_last_error() == JSON_ERROR_NONE)
  </code></pre>
  
  <p>Though I think it's useless because it will never be valid if the cURL response is null or empty. When this code triggers, the json error code is 3 (JSON_ERROR_CTRL_CHAR)</p>
  
  <h3>The problematic code:</h3>
  
  <pre><code>function apiCall($url) {
     ...
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $response = curl_exec($ch);
  }
  </code></pre>
  
  <pre><code>$inc = 0;
  $url = 'https://api.example.com/' . $id;
  $jsonString = apiCall($url);
  
  if(!is_null($jsonString)) {
  file_put_contents('pathToDirectory/' . $id + $inc, $jsonString);
  $nextUrl = getNextUrl($jsonString);
  
      while ($nextUrl) {
          $jsonString = apiCall($url . '?page=' . $nextUrl);
  
          if(!is_null($jsonString)) {
              $inc++;
              file_put_contents('pathToDirectory/' . $id + $inc, $jsonString);
              $nextUrl = getNextUrl($jsonString);
          }
      }
  }
  </code></pre>
  
  <h3>What I'm expecting my code to do:</h3>
  
  <p>Not stop randomly, or at least give me a clear error code.</p>