gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:51:35 30 4
gpt4 key购买 nike

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

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

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

最佳答案

如果您使用的是 Guzzlehttp6:

  • 如果您想为给定类型的协议(protocol)指定不同的代理:

     $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
    ]
    ]);
  • 如果您想为所有查询禁用代理:

    $client->request('GET','your_url_here',['proxy'=>'']);

    Link to the official documentation.

关于php - 使用 Guzzle HTTP 客户端时如何禁用代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23697205/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com