Extended Host Configuration
editExtended Host Configuration
editThe client also supports an extended host configuration syntax. The inline configuration method relies on PHP’s
filter_var() and parse_url() methods to validate and extract the components of a URL. Unfortunately, these built-in
methods run into problems with certain edge-cases. For example, filter_var() will not accept URLs that have underscores
(which are questionably legal, depending on how you interpret the RFCs). Similarly, parse_url() will choke if a
Basic Auth’s password contains special characters such as a pound sign (#) or question-marks (?).
For this reason, the client supports an extended host syntax which provides greater control over host initialization. None of the components are validated, so edge-cases like underscores domain names will not cause problems.
The extended syntax is an array of parameters for each host:
$hosts = [
// This is effectively equal to: "https://username:password!#$?*abc@foo.com:9200/"
[
'host' => 'foo.com',
'port' => '9200',
'scheme' => 'https',
'user' => 'username',
'password' => 'password!#$?*abc'
],
// This is equal to "http://localhost:9200/"
[
'host' => 'localhost', // Only host is required
]
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build(); // Build the client object
Only the host parameter is required for each configured host. If not provided, the default port is 9200. The default
scheme is http.