expand($template, $variables); } /** * Wrapper for JSON decode that implements error detection with helpful * error messages. * * @param string $json JSON data to parse * @param bool $assoc When true, returned objects will be converted * into associative arrays. * @param int $depth User specified recursion depth. * @param int $options Bitmask of JSON decode options. * * @return mixed * @throws \InvalidArgumentException if the JSON cannot be parsed. * @link http://www.php.net/manual/en/function.json-decode.php */ public static function jsonDecode($json, $assoc = false, $depth = 512, $options = 0) { if ($json === '' || $json === null) { return null; } static $jsonErrors = [ JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' ]; $data = \json_decode($json, $assoc, $depth, $options); if (JSON_ERROR_NONE !== json_last_error()) { $last = json_last_error(); throw new \InvalidArgumentException( 'Unable to parse JSON data: ' . (isset($jsonErrors[$last]) ? $jsonErrors[$last] : 'Unknown error') ); } return $data; } /** * Get the default User-Agent string to use with Guzzle * * @return string */ public static function getDefaultUserAgent() { static $defaultAgent = ''; if (!$defaultAgent) { $defaultAgent = 'Guzzle/' . ClientInterface::VERSION; if (extension_loaded('curl')) { $defaultAgent .= ' curl/' . curl_version()['version']; } $defaultAgent .= ' PHP/' . PHP_VERSION; } return $defaultAgent; } /** * Create a default handler to use based on the environment * * @throws \RuntimeException if no viable Handler is available. */ public static function getDefaultHandler() { $default = $future = null; if (extension_loaded('curl')) { $config = [ 'select_timeout' => getenv('GUZZLE_CURL_SELECT_TIMEOUT') ?: 1 ]; if ($maxHandles = getenv('GUZZLE_CURL_MAX_HANDLES')) { $config['max_handles'] = $maxHandles; } if (function_exists('curl_reset')) { $default = new CurlHandler(); $future = new CurlMultiHandler($config); } else { $default = new CurlMultiHandler($config); } } if (ini_get('allow_url_fopen')) { $default = !$default ? new StreamHandler() : Middleware::wrapStreaming($default, new StreamHandler()); } elseif (!$default) { throw new \RuntimeException('Guzzle requires cURL, the ' . 'allow_url_fopen ini setting, or a custom HTTP handler.'); } return $future ? Middleware::wrapFuture($default, $future) : $default; } }