如何在 Laravel 7 中的 Http 请求中设置代理?

Jos*_*hme 3 api proxy web-services http laravel

下面是我在DEV环境下成功的HTTP请求:

$response = Http::withHeaders([
                        'Content-Type' => 'application/json',
                        'Accept' => 'application/json'
                    ])
                    ->withToken('xxxxxxxxxxxxxx')
                    ->post('https://xxxxxxxxx.com/v0.1/messages/', [
                        'from' => [
                            'type' => 'xxxx',
                            'number' => 'xxxxxxxx',

                        ],
                        'to' => [
                            'type' => 'xxxxx',
                            'number' => 'xxxxxx',
                        ],
                        'message' => [
                            'content' => [
                                'type' => 'text',
                                'text' => 'test message from laravel'
                            ]
                        ]
                    ]);
Run Code Online (Sandbox Code Playgroud)

但在生产中,必须向请求添加代理。

有人知道如何通过上述请求传递代理吗?先感谢您。

dan*_*nne 6

您可以使用withOptions方法指定 Guzzle 选项。

因此:

$response = Http::withOptions([
   'proxy' => 'http://username:password@proxyhost.com:7000'
])->withHeaders( ...
Run Code Online (Sandbox Code Playgroud)