php8.5
Home/ Manual/ yar / yar_client/ Yar_Client::setOpt

Yar_Client::setOpt

PHP function Edit on GitHub ✎

(PECL yar >= 1.0.0)

Set client options

Description

Yar_Client::setOpt(int $name, mixed $value): Yar_Client|bool

Sets an option on the client. Options are evaluated when a call is issued, so they can be changed between calls.

Parameters

name

One of the YAR_OPT_* constants:

  • YAR_OPT_PACKAGER — a packager name: php, json or, when built with msgpack support, msgpack
  • YAR_OPT_PERSISTENT — a boolean persistent-connection flag (HTTP keep-alive)
  • YAR_OPT_TIMEOUT — a timeout in milliseconds, overriding yar.timeout
  • YAR_OPT_CONNECT_TIMEOUT — a connect timeout in milliseconds, overriding yar.connect_timeout
  • YAR_OPT_HEADER (as of 2.0.4) — an Array of additional HTTP header lines
  • YAR_OPT_RESOLVE (as of 2.1.0) — an Array of hostname resolution entries
  • YAR_OPT_PROXY (as of 2.2.0) — an HTTP proxy address
  • YAR_OPT_PROVIDER (as of 2.3.0) — the provider identity, up to 32 bytes
  • YAR_OPT_TOKEN (as of 2.3.0) — the authentication token, up to 32 bytes
value

The option value. An invalid value raises a warning and makes the call return false.

Return Values

Returns the client object itself, allowing a fluent interface, or false when the option name is unknown, the value is invalid, or the option does not apply to the protocol the client was created with.

Errors/Exceptions

Conditions that make the call return false also raise a warning:

  • YAR_OPT_HEADER, YAR_OPT_RESOLVE and YAR_OPT_PROXY only work with the HTTP protocol; using them with a tcp:// or unix:// client raises a warning.
  • YAR_OPT_RESOLVE additionally requires libcurl >= 7.21.3.
  • Each option expects a specific value type (string, boolean, integer or array), described on the constants page.

Examples

Yar_Client::setOpt example

php
<?php

$client = new Yar_Client("http://api.example.com/operator.php");

/* Set timeout to 1s */
$client->setOpt(YAR_OPT_TIMEOUT, 1000);

/* Set packager to JSON */
$client->setOpt(YAR_OPT_PACKAGER, "json");

/* Set custom headers */
$client->setOpt(YAR_OPT_HEADER, ["X-Api-Key: value"]);

/* Route through an HTTP proxy */
$client->setOpt(YAR_OPT_PROXY, "127.0.0.1:8888");

/* call remote service */
$result = $client->some_method("parameter");
?>

See Also

  • Yar_Client::getOpt
  • Yar_Client::call

Source: reference/yar/yar_client/setopt.xml · from the official PHP manual (php/doc-en)