title: CURL FTP Connection refused only on hosting
body: <p>I use PHP and Curl to connect to a FTP Server,
  the weird thing is that when i try my code in my local setup, it works in both implicit TLS and Explicit TLS way, but when i run the same code on any server it doesn't work and returns <code>Connection refused</code>.</p>
  <p>The PHP code is:</p>
  <pre><code>&lt;?php
  
  function simple_list_test() {
      $curlopts = [];
      $debug = true;
      $return = null;
      
      $curlopts[CURLOPT_USERPWD] = &quot;{$user}:{$password}&quot;;
      $curlopts[CURLOPT_SSL_VERIFYPEER] = false;
      $curlopts[CURLOPT_SSL_VERIFYHOST] = false;
      $curlopts[CURLOPT_FTP_SSL] = CURLFTPSSL_TRY;
      $curlopts[CURLOPT_FTPSSLAUTH] = CURLFTPAUTH_TLS;
      $curlopts[CURLOPT_RETURNTRANSFER] = true;
      $curlopts[CURLOPT_URL] = &quot;ftp://ftp.avidafinance.com:21/&quot;; // I tried with ftps:// protocol too it works on local but not when you run it on a hosting
      $curlopts[CURLOPT_FTPLISTONLY] = 1;
      $curlopts[CURLOPT_UPLOAD] = 0;
      $curlopts[CURLOPT_RETURNTRANSFER] = 1;
  
      $ch = curl_init();
  
      foreach($curlopts as $key =&gt; $value) {
          curl_setopt($ch, $key, $value);
      }
  
      if ($debug) {
          curl_setopt($ch, CURLOPT_VERBOSE, true);
          $verbose = fopen('php://temp', 'w+');
          curl_setopt($ch, CURLOPT_STDERR, $verbose);
      }
  
      $return = curl_exec($ch);
  
      if ($debug) {
          rewind($verbose);
          $verboseLog = stream_get_contents($verbose);
          echo &quot;Verbose information:\n&lt;pre&gt;&quot;, htmlspecialchars($verboseLog), &quot;&lt;/pre&gt;\n&quot;;
      }
  
      if ($error = curl_error($ch)) {
          throw new Exception($error);
      }
  
      return $return;
  }
  </code></pre>
  <p>I spent an entire day, trying so many methods including setting <code>CURLOPT_PORT</code> , setting <code>ftps://</code> protocol, increasing timeout, including the server <code>.crt</code> and <code>.pem</code> certificates with curl, but none of them worked so i was wondering if anyone can give me a hand here,</p>
  <p>Here's a brief of verbose output on local dev environment with PHP 7.3 and curl 7.68.0:</p>
  <pre><code>*   Trying 13.53.118.182:21...
  * TCP_NODELAY set
  * Connected to ftp.avidafinance.com (13.53.118.182) port 21 (#0)
  &lt; 220 FileZilla Server 0.9.60 beta
  &gt; AUTH TLS
  &lt; 234 Using authentication type TLS
  * successfully set certificate verify locations:
  *   CAfile: /etc/ssl/certs/ca-certificates.crt
    CApath: /etc/ssl/certs
  * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
  * Server certificate:
  *  subject: OU=Domain Control Validated; CN=ftp.avidafinance.com
  *  start date: Mar 23 12:46:23 2020 GMT
  *  expire date: Mar 23 12:46:23 2022 GMT
  *  issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
  *  SSL certificate verify ok.
  &gt; USER ****
  &lt; 331 Password required for *****
  &gt; PASS *****
  &lt; 230 Logged on
  &gt; PBSZ 0
  &lt; 200 PBSZ=0
  &gt; PROT P
  &lt; 200 Protection level set to P
  &gt; PWD
  &lt; 257 &quot;/&quot; is current directory.
  * Entry path is '/'
  * Request has same path as previous transfer
  &gt; EPSV
  * Connect data stream passively
  * ftp_perform ends with SECONDARY: 0
  &lt; 229 Entering Extended Passive Mode (|||57081|)
  *   Trying 13.53.118.182:57081...
  * TCP_NODELAY set
  * Connecting to 13.53.118.182 (13.53.118.182) port 57081
  * Connected to ftp.avidafinance.com (13.53.118.182) port 21 (#0)
  &gt; TYPE A
  &lt; 200 Type set to A
  &gt; NLST
  &lt; 150 Opening data channel for directory listing of &quot;/&quot;
  * Maxdownload = -1
  * Doing the SSL/TLS handshake on the data stream
  * successfully set certificate verify locations:
  *   CAfile: /etc/ssl/certs/ca-certificates.crt
    CApath: /etc/ssl/certs
  * SSL re-using session ID
  * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
  * Server certificate:
  *  subject: OU=Domain Control Validated; CN=ftp.avidafinance.com
  *  start date: Mar 23 12:46:23 2020 GMT
  *  expire date: Mar 23 12:46:23 2022 GMT
  *  issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
  *  SSL certificate verify ok.
  * Remembering we are in dir &quot;&quot;
  &lt; 226 Successfully transferred &quot;/&quot;
  * Connection #0 to host ftp.avidafinance.com left intact
  </code></pre>
  <p>But when i run the exact same code on a host ( tried more than 3 different servers ), it's gonna be like this :</p>
  <pre><code>*   Trying 13.53.118.182...
  * TCP_NODELAY set
  * Connected to ftp.avidafinance.com (13.53.118.182) port 21 (#0)
  &lt; 220 FileZilla Server 0.9.60 beta
  &gt; AUTH TLS
  &lt; 234 Using authentication type TLS
  * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
  * successfully set certificate verify locations:
  *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
    CApath: none
  * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
  * Server certificate:
  *  subject: OU=Domain Control Validated; CN=ftp.avidafinance.com
  *  start date: Mar 23 12:46:23 2020 GMT
  *  expire date: Mar 23 12:46:23 2022 GMT
  *  issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
  *  SSL certificate verify ok.
  &gt; USER *****
  &lt; 331 Password required for ******
  &gt; PASS *****
  &lt; 230 Logged on
  &gt; PBSZ 0
  &lt; 200 PBSZ=0
  &gt; PROT P
  &lt; 200 Protection level set to P
  &gt; PWD
  &lt; 257 &quot;/&quot; is current directory.
  * Entry path is '/'
  &gt; EPSV
  * Connect data stream passively
  * ftp_perform ends with SECONDARY: 0
  &lt; 229 Entering Extended Passive Mode (|||57093|)
  *   Trying 13.53.118.182...
  * TCP_NODELAY set
  * Connecting to 13.53.118.182 (13.53.118.182) port 57093
  * connect to 13.53.118.182 port 21 failed: Connection refused
  * Failed to connect to ftp.avidafinance.com port 21: Connection refused
  * Failed EPSV attempt. Disabling EPSV
  &gt; PASV
  &lt; 227 Entering Passive Mode (13,53,118,182,222,194)
  *   Trying 13.53.118.182...
  * TCP_NODELAY set
  * Connecting to 13.53.118.182 (13.53.118.182) port 57026
  * connect to 13.53.118.182 port 21 failed: Connection refused
  * Failed to connect to ftp.avidafinance.com port 21: Connection refused
  * Closing connection 0
  </code></pre>
  <p>In Short i need an <code>Explicit FTP Over TLS</code> connection, i tried with <code>curl</code>, <code>ftp_ssl_connect</code> <code>fsockets</code>, .... none of them worked, I would appreciate any help, it doesn't have to necessarily be with curl</p>