首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有使用代理的AMPHP HTTP客户端

具有使用代理的AMPHP HTTP客户端
EN

Stack Overflow用户
提问于 2020-06-29 14:51:17
回答 1查看 426关注 0票数 0

我正在尝试将AMPHP HTTP-Client与代理一起使用,但我无法使其工作。

我使用的是他们的GitHub中的示例。(https://github.com/amphp/http-tunnel/blob/master/examples/http-client-via-proxy.php)

我必须下载10个URL,并为每个URL使用不同的代理。当前的问题是,它返回这种类型的错误:

代码语言:javascript
复制
TLS negotiation failed: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
  error:1408F10B:SSL routines:ssl3_get_record:wrong version number

我们的代理服务器使用证书(.crx)进行操作。我不需要检查SSL是否有效,我只想跳过验证,所以我认为这些行可以做我需要的事情(跳过验证),但它们不...?

代码语言:javascript
复制
$clientTlsContext = new ClientTlsContext('');
$clientTlsContext->withoutPeerVerification();
$clientTlsContext->withSecurityLevel(0);

这适用于curl:

代码语言:javascript
复制
curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlResource, CURLOPT_SSL_VERIFYHOST, 0);

这是我的代码:

代码语言:javascript
复制
class AMPHPDownloaderTest
{
    /**
     * @param ConfigWithCallback[] $configsWithCallback
     */
    public static function downSerps($configsWithCallback): void
    {
        Loop::run(static function () use ($configsWithCallback) {
            try {
                $clientTlsContext = new ClientTlsContext('');
                $clientTlsContext->withoutPeerVerification();
                $clientTlsContext->withSecurityLevel(0);

                $connector = new Https1TunnelConnector(new SocketAddress('proxyi2.infatica.io', 44123), $clientTlsContext);

                $client = (new HttpClientBuilder)
                    ->usingPool(new UnlimitedConnectionPool(new DefaultConnectionFactory($connector)))
                    ->build();

                $request = new Request('http://amphp.org/');

                /** @var Response $response */
                $response = yield $client->request($request);

                $request = $response->getRequest();

                \printf(
                    "%s %s HTTP/%s\r\n",
                    $request->getMethod(),
                    $request->getUri(),
                    \implode('+', $request->getProtocolVersions())
                );

                print Rfc7230::formatHeaders($request->getHeaders()) . "\r\n\r\n";

                \printf(
                    "HTTP/%s %d %s\r\n",
                    $response->getProtocolVersion(),
                    $response->getStatus(),
                    $response->getReason()
                );

                print Rfc7230::formatHeaders($response->getHeaders()) . "\r\n\r\n";

                $body = yield $response->getBody()->buffer();
                $bodyLength = \strlen($body);

                if ($bodyLength < 250) {
                    print $body . "\r\n";
                } else {
                    print \substr($body, 0, 250) . "\r\n\r\n";
                    print($bodyLength - 250) . " more bytes\r\n";
                }
            } catch (HttpException $error) {
                echo $error;
            }
        });
    }
}

当与Http1TunnelConnector而不是Https1TunnelConnector一起使用时,它会抛出以下错误:

代码语言:javascript
复制
Amp\Socket\TlsException: TLS negotiation failed: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
EN

回答 1

Stack Overflow用户

发布于 2020-07-05 15:36:00

您基本上是在做正确的事情,但是ClientTlsContext是不可变的,并且总是返回一个新的实例,该实例在您的代码示例中被丢弃。

代码语言:javascript
复制
$clientTlsContext = (new ClientTlsContext(''))
    ->withoutPeerVerification()
    ->withSecurityLevel(0);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62632675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档