如何在使用Guzzle HTTP客户端时禁用代理?

时间:2014-05-16 14:02:20

标签: php proxy guzzle

我正在尝试使用Guzzle库在客户端服务器上调用Web服务 - 但是服务器有一个代理,所以我的代码中出现404错误。

如果我进入客户端服务器并尝试

wget http://www.mywebsite.com/mywebservice

我收到错误

Resolving proxy.theirdomainname.com (proxy.theirdomainname.com)... xx.xx.xx.xx
Connecting to proxy.theirdomainname.com (proxy.theirdomainname.com)|xx.xx.xx.xx|:80...
failed: Connection timed out.

但如果我使用

wget --no-proxy http://www.mywebsite.com/mywebservice

我得到了一个结果

Resolving www.mywebsite.com (www.mywebsite.com)... xx.xx.xx.xx
Connecting to www.mywebsite.com (www.mywebsite.com)|xx.xx.xx.xx|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]

我可以看到在Guzzle文档中设置代理的选项 - http://guzzle.readthedocs.org/en/latest/http-client/client.html#proxy

但是如何完全禁用代理呢?或者这是一个服务器设置?

编辑:

$request = $client->get($this->url(), array('proxy' => ''))
                  ->setHeader('Accept', 'text/xml');

$response = $request->send();
var_dump($response);

结果:

Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException'
with message 'Client error response [status code] 404 [reason phrase] Not Found [url] http://mywebsite.com'
in /vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php:44 
Stack trace: #0 /guzzle/guzzle/src/Guzzle/Http/Message/Request.php(145): Guzzle\Http\Exception\BadResponseException::factory(Object(Guzzle\Http\Message\Request), Object(Guzzle\Http\Message\Response)) 
#1 [internal function]: Guzzle\Http\Message\Request::onRequestError(Object(Guzzle\Common\Event))
#2 /vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(164): call_user_func(Array, Object(Guzzle\Common\Event))
#3 /vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(53): Symfony in /vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php on line 44

2 个答案:

答案 0 :(得分:3)

如果您使用的是Guzzlehttp6:

  • 如果要为给定类型的协议指定不同的代理:

    ;This procedure bind a symbol `f` to a lambda, and that lambda is not evaluated.
    (define f
      (lambda () x))
    
    ;create a frame which binds x to 10, and then evaluate f, should return 10.
    (let ((x 10))
        (f))
    
  • 如果要为所有查询禁用代理:

     $client->request('GET', 'your_url_here', [
    'proxy' => [
    'http'  => 'tcp://localhost:8125', // Use this proxy with "http"
    'https' => 'tcp://localhost:9124', // Use this proxy with "https",
    'no' => ['.mit.edu', 'foo.com']    // Don't use a proxy with these
       ]
    ]);
    

    Link to the official documentation.

答案 1 :(得分:1)

这是因为Guzzle\Client自动神奇地设置代理查看您的系统环境变量。如果您使用的是Linux或Windows,则无关紧要:

  280:         // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set
  281:         if ($proxy = getenv('HTTP_PROXY')) {
  282:             $settings['proxy']['http'] = $proxy;
  283          }
  284  
  285:         if ($proxy = getenv('HTTPS_PROXY')) {
  286:             $settings['proxy']['https'] = $proxy;
  287          }

因此,如果您之前使用

之类的东西设置代理
set HTTP_PROXY=http://your.proxy.local:8080
set HTTPS_PROXY=http://your.proxy.local:8080

然后将选择此代理设置。

你必须强制执行像MarcellFülöp这样的空代理。