使用PHP通过身份验证代理检索URL


Retrieving a URL though an authenticating proxy using PHP

我试图通过需要身份验证的代理服务器编写安全(密码保护+ https) URL的检索脚本。我发现了许多如何使用"代理"选项以及如何使用stream_context_create发送凭据的示例。

$url = "https://$server/$uri";
$cred = sprintf('Authorization: Basic %s'r'n', base64_encode("$user:$pass"));
$options['header']=$cred;
$options['proxy']="tcp://10.1.1.1:3128";
$params = array('http' => $options);
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);

然而,我没有找到任何uid/密码提供给代理服务器的地方。我尝试手动添加标题:

$prox_cred = sprintf('Proxy-Connection: Keep-Alive'r'nProxy-Authorization: Basic %s'r'n', base64_encode("$proxy_user:$proxy_pass"));
$options['header'] .= $prox_cred;

我有代理服务器的根访问权限,我可以看到我的请求击中它,但是当我查看流量时,我没有看到我的头。我猜它们被添加到加密并发送到终端服务器的部分。

由于某种原因,我不想第一次使用cURL模块使事情复杂化。然而,根据我的反应,我试了一下,花了大约6分钟才使事情正常工作。

if ($proxy_server != '') {
  curl_setopt($ch,CURLOPT_PROXYAUTH,CURLAUTH_BASIC);
  curl_setopt($ch,CURLOPT_PROXY,"$proxy_server");
  if ($proxy_user != '') {
     curl_setopt($ch,CURLOPT_PROXYAUTH,CURLAUTH_BASIC);
     curl_setopt($ch,CURLOPT_PROXYUSERPWD,"$proxy_user:$proxy_pass");
  }  
}  

谢谢!