Last active
April 12, 2019 17:04
-
-
Save iyaozhen/0248148350b3ac185375af80d621f8e4 to your computer and use it in GitHub Desktop.
PHP get sys http proxy conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* get sys http proxy conf | |
* base from system PATH,and Windows get from Internet Settings | |
* | |
* @return array | |
*/ | |
function getHttpProxyConf() | |
{ | |
$httpProxy = ''; | |
$httpsProxy = ''; | |
$proxyEnv = getenv('HTTP_PROXY'); | |
if (php_sapi_name() == 'cli' && $proxyEnv !== false) { | |
$httpProxy = $proxyEnv; | |
} | |
$proxyEnv = getenv('HTTPS_PROXY'); | |
if ($proxyEnv !== false) { | |
$httpsProxy = $proxyEnv; | |
} | |
if (empty($httpProxy) && empty($httpsProxy) && strpos(PHP_OS, 'WIN') !== false) { | |
/** | |
* @link https://stackoverflow.com/a/12618950 | |
*/ | |
$proxyServer = shell_exec('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | find /i "proxyserver"'); | |
if (!empty($proxyServer)) { | |
if (preg_match("/ProxyServer *REG_SZ *?(.+)/i", $proxyServer, $matches)) { | |
$proxyConf = explode(';', $matches[1]); | |
foreach ($proxyConf as $item) { | |
if (preg_match("/http=.+/", $item)) { | |
$httpProxy = str_replace('http=', 'http://', $item); | |
} | |
if (preg_match("/https=.+/", $item)) { | |
$httpsProxy = str_replace('https=', 'https://', $item);; | |
} | |
} | |
} | |
} | |
} | |
return [ | |
'http' => trim($httpProxy), | |
'https' => trim($httpsProxy), | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment