HttpClient.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient;
  11. use Amp\Http\Client\Connection\ConnectionLimitingPool;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. /**
  14. * A factory to instantiate the best possible HTTP client for the runtime.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. final class HttpClient
  19. {
  20. /**
  21. * @param array $defaultOptions Default request's options
  22. * @param int $maxHostConnections The maximum number of connections to a single host
  23. * @param int $maxPendingPushes The maximum number of pushed responses to accept in the queue
  24. *
  25. * @see HttpClientInterface::OPTIONS_DEFAULTS for available options
  26. */
  27. public static function create(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface
  28. {
  29. if ($amp = class_exists(ConnectionLimitingPool::class)) {
  30. if (!\extension_loaded('curl')) {
  31. return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
  32. }
  33. // Skip curl when HTTP/2 push is unsupported or buggy, see https://bugs.php.net/77535
  34. if (\PHP_VERSION_ID < 70217 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304) || !\defined('CURLMOPT_PUSHFUNCTION')) {
  35. return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
  36. }
  37. static $curlVersion = null;
  38. $curlVersion = $curlVersion ?? curl_version();
  39. // HTTP/2 push crashes before curl 7.61
  40. if (0x073d00 > $curlVersion['version_number'] || !(\CURL_VERSION_HTTP2 & $curlVersion['features'])) {
  41. return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
  42. }
  43. }
  44. if (\extension_loaded('curl')) {
  45. if ('\\' !== \DIRECTORY_SEPARATOR || ini_get('curl.cainfo') || ini_get('openssl.cafile') || ini_get('openssl.capath')) {
  46. return new CurlHttpClient($defaultOptions, $maxHostConnections, $maxPendingPushes);
  47. }
  48. @trigger_error('Configure the "curl.cainfo", "openssl.cafile" or "openssl.capath" php.ini setting to enable the CurlHttpClient', \E_USER_WARNING);
  49. }
  50. if ($amp) {
  51. return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
  52. }
  53. @trigger_error((\extension_loaded('curl') ? 'Upgrade' : 'Install').' the curl extension or run "composer require amphp/http-client" to perform async HTTP operations, including full HTTP/2 support', \E_USER_NOTICE);
  54. return new NativeHttpClient($defaultOptions, $maxHostConnections);
  55. }
  56. /**
  57. * Creates a client that adds options (e.g. authentication headers) only when the request URL matches the provided base URI.
  58. */
  59. public static function createForBaseUri(string $baseUri, array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface
  60. {
  61. $client = self::create([], $maxHostConnections, $maxPendingPushes);
  62. return ScopingHttpClient::forBaseUri($client, $baseUri, $defaultOptions);
  63. }
  64. }