title: php curl behaves differently under browser & CLI due to proxy
body: <p>Initially I was having issues trying to figure out why php curl under browser behaves differently when I tried to execute the same script by CLI.</p>
  <p>By turning on the CURLOPT_VERBOSE with log output and compare the result of the CLI and browser, here are the differences I've seen:</p>
  <p><strong>CURL Under CLI</strong></p>
  <pre><code>* About to connect() to proxy localhost port 3128 (#4)
  *   Trying ::1...
  * Connection refused
  *   Trying 127.0.0.1...
  * Connected to localhost (127.0.0.1) port 3128 (#4)
  * Establish HTTP proxy tunnel to someurl.com:443
  * Server auth using Basic with user 'some_username'
  &gt; CONNECT someurl.com:443 HTTP/1.1
  Host: someurl.com:443
  Proxy-Connection: Keep-Alive
  
  &lt; HTTP/1.1 407 Proxy Authentication Required
  &lt; Mime-Version: 1.0
  &lt; Date: Fri, 11 Dec 2020 12:04:46 CST
  &lt; Via: 1.1 someotherurl.com:8080 (Cisco-WSA/12.0.1-334)
  &lt; Content-Type: text/html
  &lt; Connection: close
  &lt; Proxy-Connection: close
  &lt; Content-Length: 2109
  &lt; X-RBT-SCAR: 2.3.4.5:11517381:2000
  &lt; Proxy-Authenticate: Basic realm=&quot;Cntlm for parent&quot;
  * Authentication problem. Ignoring this.
  &lt; 
  * Received HTTP code 407 from proxy after CONNECT
  * Connection #4 to host localhost left intact
  </code></pre>
  <p><strong>CURL Under Browser</strong></p>
  <pre><code>* About to connect() to someurl.com port 443 (#6)
  *   Trying 1.2.3.4...
  * Connected to someurl.com (1.2.3.4) port 443 (#6)
  * warning: ignoring value of ssl.verifyhost
  * skipping SSL peer certificate verification
  * SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  * Server certificate:
  *   subject: C=US,ST=FL,L=Boca Raton,O=Telit IoT Platforms,OU=secureWISE,CN=someurl.com
  *   start date: Apr 15 21:18:15 2020 GMT
  *   expire date: May 15 21:18:15 2022 GMT
  *   common name: someurl.com
  *   issuer: E=support@securewise.net,CN=secureWISE CA-256,OU=SecureWISE Certificate Authority,O=ILS Technology LLC,O=Telit Wireless Solutions Inc,L=Boca Raton,ST=Florida,C=US
  * Server auth using Basic with user 'some_username'
  &gt; GET /someurl HTTP/1.1
  Authorization: Basic SomeAuthKey
  Host: someurl.com
  Accept: */*
  
  &lt; HTTP/1.1 200 OK
  &lt; Date: Fri, 11 Dec 2020 04:07:40 GMT
  &lt; Server: Apache-Coyote/1.1
  &lt; X-Powered-By: Undertow/1
  &lt; Set-Cookie: JSESSIONID=c2BBPwZBjGxCaH5om6unoKaI; path=/
  &lt; Set-Cookie: somekey=somevalue; path=/
  &lt; Content-Type: text/xml
  &lt; Content-Length: 125291
  &lt; Content-disposition: attachment; filename=somefilename.xml
  &lt; Vary: Accept-Encoding,User-Agent
  &lt; SWOrigin: sw_proxy
  &lt; Connection: close
  &lt; 
  * Closing connection 6
  </code></pre>
  <p>My initial hunch is that this has something to do with proxy (as this PC does use a proxy to go online)</p>
  <p>And looking at the browser log, it seems as if proxy was skipped.</p>
  <p>I've also checked the phpinfo() for both the browser and CLI, and I can see that there's <code>proxy</code>, <code>http_proxy</code>, <code>https_proxy</code> defined in the environment variables, as well as under <code>$_SERVER</code> for <strong>CLI</strong>, but <strong>not on browser</strong>, which makes me believe more that my assumption is correct.</p>
  <p>So in order to combat this, I've tried adding the following code before the curl call:</p>
  <pre><code>        if(isset($_SERVER['http_proxy']))
              unset($_SERVER['http_proxy']);       
          if (isset($_SERVER['https_proxy']))
              unset($_SERVER['https_proxy']);
          if (isset($_SERVER['proxy']))
              unset($_SERVER['proxy']);
  
          if(isset($_ENV['http_proxy']))
              unset($_ENV['http_proxy']);       
          if (isset($_ENV['https_proxy']))
              unset($_ENV['https_proxy']);
          if (isset($_ENV['proxy']))
              unset($_ENV['proxy']);
  
          curl_setopt($ch, CURLOPT_URL, $target_url);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_USERPWD, &quot;someuser:somepass&quot;);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($ch, CURLOPT_TIMEOUT, 10);
          curl_setopt($ch, CURLOPT_VERBOSE, true);
          $result = curl_exec($ch); 
          curl_close($ch);
  </code></pre>
  <p>But the verbose still shows that it still tries to go through the proxy when executed under CLI.</p>
  <p>Any suggestion on this?</p>