Skip to content

Instantly share code, notes, and snippets.

@iyaozhen
Last active April 12, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iyaozhen/0248148350b3ac185375af80d621f8e4 to your computer and use it in GitHub Desktop.
Save iyaozhen/0248148350b3ac185375af80d621f8e4 to your computer and use it in GitHub Desktop.
PHP get sys http proxy conf
<?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