HttpClient.php 3.5 KB

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