开发者

drupal_http_request without proxy support on drupal 6... ideas how to do it?

I brought up some proxy servers a couple of days ago, but drupal_http_request does not support calling 开发者_开发知识库a URL through a proxy. Anyone have a patch or solution? (drupal 6)


You could use Curl instead of drupal_http_request. That would allow you to specify a proxy.


In Drupal 6, drupal_http_request() doesn't support proxies, but it does in Drupal 7, through the following code.

  // Use a proxy if one is defined and the host is not on the excluded list.
  $proxy_server = variable_get('proxy_server', '');
  if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
    // Set the scheme so we open a socket to the proxy server.
    $uri['scheme'] = 'proxy';
    // Set the path to be the full URL.
    $uri['path'] = $url;
    // Since the URL is passed as the path, we won't use the parsed query.
    unset($uri['query']);

    // Add in username and password to Proxy-Authorization header if needed.
    if ($proxy_username = variable_get('proxy_username', '')) {
      $proxy_password = variable_get('proxy_password', '');
      $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
    }
    // Some proxies reject requests with any User-Agent headers, while others
    // require a specific one.
    $proxy_user_agent = variable_get('proxy_user_agent', '');
    // The default value matches neither condition.
    if ($proxy_user_agent === NULL) {
      unset($options['headers']['User-Agent']);
    }
    elseif ($proxy_user_agent) {
      $options['headers']['User-Agent'] = $proxy_user_agent;
    }
  }

The alternative of using cUrl is then to execute code similar to the following one.

$headers = array(
  // The user agent to use for the proxy, or NULL.
  'User-Agent' => $proxy_user_agent,

  // The host to call.
  'Host' => $host,

  // Use this only if required.
  'Proxy-Authorization' => 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : '')),
);

$result = drupal_http_request($proxy_url, $headers, $method, $data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜