dongxiezhi0590
dongxiezhi0590
2019-06-05 14:43

如何在Symfony HttpClient组件的CurlHttpClient中添加具有IP /端口和用户名/密码的代理?

已采纳

I'm using Symfony HttpClient Component, it became necessary to use it with a proxy. Open proxy with the port, I can easily configure with the default configuration when instantiating CurlHttpClient class, but I can't set username and password for private proxy

Example of default configuration:

use Symfony\Component\HttpClient\CurlHttpClient;

$httpClient = new CurlHttpClient([
    'http_version' => '2.0',
    'proxy' => 'x.xx.xxx.xxx:8000',
]);

I try to add additional configuration on the extra options, and try other combinations, but can't find the right way.

with plain PHP and cURL everything works fine.

Please, help me, configure the Symfony HttpClient Component with a private proxy.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanjunao9348 duanjunao9348 2年前

    You need to specify protocol (e.g. socks5, socks4 etc)

    use Symfony\Component\HttpClient\CurlHttpClient;
    
    $httpClient = new CurlHttpClient([
        'http_version' => '2.0',
        'proxy' => 'socks5://username:password@127.0.0.1:9999',
    ]);
    
    dump($httpClient->request('GET', 'https://api.ipify.org?format=json')->getContent());
    
    点赞 评论 复制链接分享
  • dougaxing8673 dougaxing8673 2年前

    I found the solution, the data for the proxy should be specified in the following format:

    $httpClient = new CurlHttpClient([
        'http_version' => '2.0',
        'proxy' => 'user:password@ip:port',
    ]);
    

    it wors fine

    点赞 评论 复制链接分享