簡體   English   中英

具有使用代理的 AMPHP HTTP 客戶端

[英]AMPHP HTTP Client with usage proxies

我正在嘗試將 AMPHP HTTP-Client 與代理一起使用,但我無法使其工作。

我正在使用他們 GitHub 中的示例。 https://github.com/amphp/http-tunnel/blob/master/examples/http-client-via-proxy.php

我必須下載 10 個 URL 並為每個 URL 使用不同的代理。 當前的問題是它返回這種錯誤:

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 是否有效,我只想跳過驗證,所以我認為這些行可以滿足我的需要(跳過驗證),但它們沒有......

$clientTlsContext = new ClientTlsContext('');
$clientTlsContext->withoutPeerVerification();
$clientTlsContext->withSecurityLevel(0);

這適用於 curl:

curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlResource, CURLOPT_SSL_VERIFYHOST, 0);

這是我的代碼:

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 一起使用時,它會引發此錯誤:

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

您基本上是在做正確的事情,但是ClientTlsContext是不可變的,並且始終返回一個新實例,該實例在您的代碼示例中被丟棄。

$clientTlsContext = (new ClientTlsContext(''))
    ->withoutPeerVerification()
    ->withSecurityLevel(0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM