2019-01-27 20:00:39 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\HttpClient ;
2020-03-02 14:36:02 +00:00
use Amp\Http\Client\Connection\ConnectionLimitingPool ;
2019-01-27 20:00:39 +00:00
use Symfony\Contracts\HttpClient\HttpClientInterface ;
/**
* A factory to instantiate the best possible HTTP client for the runtime .
*
* @ author Nicolas Grekas < p @ tchwork . com >
*/
final class HttpClient
{
/**
2020-03-14 18:44:21 +00:00
* @ param array $defaultOptions Default request ' s options
2019-01-27 20:00:39 +00:00
* @ param int $maxHostConnections The maximum number of connections to a single host
2019-04-04 15:02:57 +01:00
* @ param int $maxPendingPushes The maximum number of pushed responses to accept in the queue
2019-01-27 20:00:39 +00:00
*
* @ see HttpClientInterface :: OPTIONS_DEFAULTS for available options
*/
2019-04-04 15:02:57 +01:00
public static function create ( array $defaultOptions = [], int $maxHostConnections = 6 , int $maxPendingPushes = 50 ) : HttpClientInterface
2019-01-27 20:00:39 +00:00
{
2020-03-02 14:36:02 +00:00
if ( $amp = class_exists ( ConnectionLimitingPool :: class )) {
if ( ! \extension_loaded ( 'curl' )) {
return new AmpHttpClient ( $defaultOptions , null , $maxHostConnections , $maxPendingPushes );
}
// Skip curl when HTTP/2 push is unsupported or buggy, see https://bugs.php.net/77535
if ( \PHP_VERSION_ID < 70217 || ( \PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304 ) || ! \defined ( 'CURLMOPT_PUSHFUNCTION' )) {
return new AmpHttpClient ( $defaultOptions , null , $maxHostConnections , $maxPendingPushes );
}
static $curlVersion = null ;
$curlVersion = $curlVersion ? ? curl_version ();
// HTTP/2 push crashes before curl 7.61
if ( 0x073d00 > $curlVersion [ 'version_number' ] || ! ( CURL_VERSION_HTTP2 & $curlVersion [ 'features' ])) {
return new AmpHttpClient ( $defaultOptions , null , $maxHostConnections , $maxPendingPushes );
}
}
2019-01-27 20:00:39 +00:00
if ( \extension_loaded ( 'curl' )) {
2019-06-13 12:53:55 +01:00
if ( '\\' !== \DIRECTORY_SEPARATOR || ini_get ( 'curl.cainfo' ) || ini_get ( 'openssl.cafile' ) || ini_get ( 'openssl.capath' )) {
return new CurlHttpClient ( $defaultOptions , $maxHostConnections , $maxPendingPushes );
}
@ trigger_error ( 'Configure the "curl.cainfo", "openssl.cafile" or "openssl.capath" php.ini setting to enable the CurlHttpClient' , E_USER_WARNING );
2019-01-27 20:00:39 +00:00
}
2020-03-02 14:36:02 +00:00
if ( $amp ) {
return new AmpHttpClient ( $defaultOptions , null , $maxHostConnections , $maxPendingPushes );
}
2020-03-19 02:46:30 +00:00
@ 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 );
2019-04-04 15:02:57 +01:00
return new NativeHttpClient ( $defaultOptions , $maxHostConnections );
2019-01-27 20:00:39 +00:00
}
2019-10-13 18:51:09 +01:00
/**
* Creates a client that adds options ( e . g . authentication headers ) only when the request URL matches the provided base URI .
*/
public static function createForBaseUri ( string $baseUri , array $defaultOptions = [], int $maxHostConnections = 6 , int $maxPendingPushes = 50 ) : HttpClientInterface
{
$client = self :: create ([], $maxHostConnections , $maxPendingPushes );
return ScopingHttpClient :: forBaseUri ( $client , $baseUri , $defaultOptions );
}
2019-01-27 20:00:39 +00:00
}