title: PHP curl unable to access other servers via VPN
body: <p>I have a server connected to my company via OpenVPN and I am trying to retrieve JSON from another server using PHP's curl command.</p>
  
  <p>Running the most stripped back version of the code in PHP's interactive mode from the command line (php -a), works fine:</p>
  
  <pre><code>$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'status.int.companyname.com');
  curl_exec($ch);
  {...expected json response after 2 seconds}
  </code></pre>
  
  <p>Howver when I run this from within Apache, the script will just hang. </p>
  
  <p>Setting some timeouts and outputting the error</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  echo 'Error: '.curl_error($ch);
  </code></pre>
  
  <p>Gives the error as</p>
  
  <pre><code>Error: Resolving timed out after 10517 milliseconds
  </code></pre>
  
  <p>Initially I thought this was a DNS-related issue, but changing the URL to use the IP address or specifying the address to resolve to</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_RESOLVE, [ 'status.int.companyname.com:80:10.10.0.5']);
  </code></pre>
  
  <p>yeilded a connection error:</p>
  
  <pre><code>Error: Connection timed out after 10001 milliseconds
  </code></pre>
  
  <p>My next thought was that Apache was using the wrong interface. The VPN is the default gateway, so works fine with everything else but I tried forcing it with</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_INTERFACE, '10.33.0.105');
  </code></pre>
  
  <p>This made no difference. After reading around a few other posts, I tried adding the following in various combinations but it made no difference.</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
  curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2);
  curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  </code></pre>
  
  <p>file_get_contents has the same problem</p>
  
  <p>I don't inderstand it. Curl on the command line works and PHP in interactive mode work fine. It just doesn't work when the PHP script is executed via Apache.</p>
  
  <p>Other things I've tried:</p>
  
  <ul>
  <li>starting Apache after the VPN is connected</li>
  <li>setting Apache to only bind to IPv4</li>
  </ul>
  
  <p>Resolv.conf contains one nameserver, which is the correct one for the company VPN</p>
  
  <p>The differences between the CLI and Apache versions of php.ini are as expected:</p>
  
  <pre><code>-disable_functions =
  +disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
  
  -expose_php = On
  +expose_php = Off
  
  -memory_limit = -1
  +memory_limit = 128M
  </code></pre>
  
  <p>Has anyone seen this before? I've lost a few hours of my day on this now and it's driving me mad!
  Thanks</p>