在drupal 6上没有代理支持的drupal_http_request ...想法怎么做?

时间:2011-02-04 19:00:51

标签: php http drupal-6 proxy

我几天前提出了一些代理服务器,但是drupal_http_request不支持通过代理调用URL。有人有补丁或解决方案吗? (drupal 6)

2 个答案:

答案 0 :(得分:1)

您可以使用Curl代替drupal_http_request。那样你就可以specify a proxy

答案 1 :(得分:0)

在Drupal 6中,drupal_http_request()不支持代理,但它在Drupal 7中支持,通过以下代码。

  // 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;
    }
  }

然后使用cUrl的替代方法是执行类似于下面的代码。

$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);