title: Twitter: OpenSSL SSL_read: Connection reset by peer, errno 104
body: <blockquote>
  <p>Twitter: OpenSSL SSL_read: Connection reset by peer, errno 104</p>
  </blockquote>
  <blockquote>
  <p>Twitter: Operation timed out after 5000 milliseconds with 0 bytes
  received</p>
  </blockquote>
  <p>This are the two errors I'm getting (randomly) while trying to upload a video file in chunks into Twitter.</p>
  <pre><code>1.5MB file = Most of the times uploads, sometimes gives error
  10MB file = Always gives error (one of the above, randomly)
  20MB file = Always gives error (one of the above, randomly)
  </code></pre>
  <p>My code is pretty much exactly like this one: <a href="https://stackoverflow.com/questions/47429149/upload-twitter-video-error-php-api-response-segments-do-not-add-up-to-provid">Upload Twitter video error (PHP). API response: Segments do not add up to provided total file size</a> and I'm using the <a href="https://github.com/abraham/twitteroauth" rel="nofollow noreferrer">TwitterOAuth</a> library.</p>
  <pre><code>function append($mediaId, $fileUrl)
  {
      $segmentIndex = 0;
  
      $handle = fopen($fileUrl, 'r');
  
      while (!feof($handle)) 
      {
          $chunk = fread($handle, 4 * 1024 * 1024); // 4mb 
  
          $params = ['command' =&gt; 'APPEND', 'media_id' =&gt; $mediaId, 'media_data' =&gt; base64_encode($chunk), 'segment_index' =&gt; $segmentIndex];
          $this-&gt;tw-&gt;uploadCustom('media/upload', $params);
  
          $segmentIndex++;
      }
  }
  </code></pre>
  <p>I know by debugging that the errors are thrown in this function.</p>
  <p>The <a href="https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-append" rel="nofollow noreferrer">documentation</a> says:</p>
  <blockquote>
  <p>&quot;media&quot; The raw binary file content being uploaded. It must be
  &lt;= 5 MB, and cannot be used with media_data.</p>
  <p>&quot;media_data&quot; The base64-encoded chunk of media file. It must be
  &lt;= 5 MB and cannot be used with media. Use raw binary (media
  parameter) when possible.</p>
  </blockquote>
  <p>I tried both options and no success. What am I missing? Why can't I upload bigger files?</p>