Documentation

CRM_Utils_GuzzleMiddleware
in package

Additional helpers/utilities for use as Guzzle middleware.

Table of Contents

Methods

authx()  : Closure
The authx middleware sends authenticated requests via JWT.
curlLog()  : mixed
This logs the list of outgoing requests in curl format.
url()  : Closure
Add this as a Guzzle handler/middleware if you wish to simplify the construction of Civi-related URLs. It enables URL schemes for:
filterUri()  : string|null

Methods

authx()

The authx middleware sends authenticated requests via JWT.

public static authx([mixed $defaults = [] ]) : Closure

To add an authentication token to a specific request, the $options must specify authx_user or authx_contact_id. Examples:

$http = new GuzzleHttp\Client(['authx_user' => 'admin']); $http->post('civicrm/admin', ['authx_user' => 'admin']);

Supported options:

  • authx_ttl (int): Seconds of validity for JWT's
  • authx_host (string): Only send tokens for the given host.
  • authx_contact_id (int): The CiviCRM contact to authenticate with
  • authx_user (string): The CMS user to authenticate with
  • authx_flow (string): How to format the auth token. One of: 'param', 'xheader', 'header'.
Parameters
$defaults : mixed = []
Return values
Closure

curlLog()

This logs the list of outgoing requests in curl format.

public static curlLog(LoggerInterface $logger) : mixed
Parameters
$logger : LoggerInterface

url()

Add this as a Guzzle handler/middleware if you wish to simplify the construction of Civi-related URLs. It enables URL schemes for:

public static url() : Closure
  • route://ROUTE_NAME (aka) route:ROUTE_NAME
  • backend://ROUTE_NAME (aka) backend:ROUTE_NAME
  • frontend://ROUTE_NAME (aka) frontend:ROUTE_NAME
  • var://PATH_EXPRESSION (aka) var:PATH_EXPRESSION
  • ext://EXTENSION/FILE (aka) ext:EXTENSION/FILE
  • assetBuilder://ASSET_NAME?PARAMS (aka) assetBuilder:ASSET_NAME?PARAMS

Compare:

$http->get(CRM_Utils_System::url('civicrm/dashboard', NULL, TRUE, NULL, FALSE, ??)) $http->get('route://civicrm/dashboard') $http->get('frontend://civicrm/dashboard') $http->get('backend://civicrm/dashboard')

$http->get(Civi::paths()->getUrl('[civicrm.files]/foo.txt')) $http->get('var:[civicrm.files]/foo.txt')

$http->get(Civi::resources()->getUrl('my.other.ext', 'foo.js')) $http->get('ext:my.other.ext/foo.js')

$http->get(Civi::service('asset_builder')->getUrl('my-asset.css', ['a'=>1, 'b'=>2])) $http->get('assetBuilder:my-asset.css?a=1&b=2')

Note: To further simplify URL expressions, Guzzle allows you to set a 'base_uri' option (which is applied as a prefix to any relative URLs). Consider using base_uri=auto:. This allows you to implicitly use the most common types (routes+variables):

$http->get('civicrm/dashboard') $http->get('[civicrm.files]/foo.txt')

Return values
Closure

filterUri()

protected static filterUri(UriInterface $oldUri) : string|null
Parameters
$oldUri : UriInterface
Return values
string|null

The string formation of the new URL, or NULL for unchanged URLs.


        
On this page

Search results