title: What is the correct way to read a large JSON API response in Guzzle 6?
body: <p>I currently have the following Guzzle 6 implementation returning a stream of JSON data containing user data:</p>
  
  <pre><code>$client = new GuzzleHttp\Client([
          'base_uri' =&gt; 'https://www.apiexample.com',
          'handler' =&gt; $oauthHandler,
          'auth'    =&gt; 'oauth',
          'headers' =&gt; [
              'Authorization' =&gt; 'Bearer xxxxxxxxxxxxxx',
              'Content-Type' =&gt; 'application/json',
              'Accept' =&gt; 'application/json',
          ],
  ]);
  
  $res = $client-&gt;post('example');
  $stream = GuzzleHttp\Psr7\stream_for($res-&gt;getBody());
  </code></pre>
  
  <p>The JSON responses look like:</p>
  
  <pre><code>{
      "name": "Users",
      "record": [
          {
              "title": "Consulting",
              "_name": "Users",
              "end date": "07/03/2020",
              "session number": "1",
              "start date": "09/02/2019",
              "course id": "2900",
              "first name": "John",
              "unique user number": "123456",
              "time": "08 AM",
              "last name": "Doe",
              "year": "19-20",
              "location name": "SD"
          },
          .........
       ],
       "@extensions": "activities,corefields,u_extension,u_stu_x,s_ncea_x,s_stu_crdc_x,c_locator,u_userfields,s_edfi_x"
   }
  </code></pre>
  
  <p>This is being run for a number of clients using different API endpoints. Many of them return too many users for the entire JSON response to be loaded into RAM at once, which is why I am using a stream. </p>
  
  <p>There may be a way to get the API to return chunks incrementally, through multiple calls. But from everything I have gotten from the developers of the API it appears that this is intended to be consumed as one streamed response.</p>
  
  <p>I am new to having to stream an API response like this and am wondering what the correct approach would be to iterate through the records? Looking at the Guzzle 6 docs it appears that the iteration happens by choosing the number x character in the string and grabbing that subsection:</p>
  
  <p><a href="http://docs.guzzlephp.org/en/stable/psr7.html#streams" rel="nofollow noreferrer">http://docs.guzzlephp.org/en/stable/psr7.html#streams</a></p>
  
  <pre><code>use GuzzleHttp\Psr7;
  
  $stream = Psr7\stream_for('string data');
  echo $stream;
  // string data
  echo $stream-&gt;read(3);
  // str
  echo $stream-&gt;getContents();
  // ing data
  var_export($stream-&gt;eof());
  // true
  var_export($stream-&gt;tell());
  // 11
  </code></pre>
  
  <p>I could potentially write something that parses the strings in subsections through pattern matching and incrementally writes the data to disk as I move through the response. But it seems like that would be error prone and something that would be part of Guzzle 6.</p>
  
  <p>Can you provide an example of how something like this should work or point out where I might be missing something? </p>
  
  <p>I appreciate it, thanks!</p>