本文整理汇总了PHP中GuzzleHttp\Event\BeforeEvent类的典型用法代码示例。如果您正苦于以下问题:PHP BeforeEvent类的具体用法?PHP BeforeEvent怎么用?PHP BeforeEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BeforeEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (file_exists($this->getFullFilePath($request))) {
$responsedata = file_get_contents($this->getFullFilePath($request));
$mf = new MessageFactory();
$event->intercept($mf->fromMessage($responsedata));
}
}
示例2: onBefore
/**
* Updates the request with an Authorization header when auth is 'fetched_auth_token'.
*
* use GuzzleHttp\Client;
* use Google\Auth\OAuth2;
* use Google\Auth\AuthTokenFetcher;
*
* $config = [..<oauth config param>.];
* $oauth2 = new OAuth2($config)
* $scoped = new AuthTokenFetcher($oauth2,
* $cache = new Memcache(),
* [ 'prefix' => 'OAuth2::' ]);
* $client = new Client([
* 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
* 'defaults' => ['auth' => 'google_auth']
* ]);
*
* $res = $client->('myproject/taskqueues/myqueue');
*/
public function onBefore(BeforeEvent $event)
{
// Requests using "auth"="google_auth" will be authorized.
$request = $event->getRequest();
if ($request->getConfig()['auth'] != 'google_auth') {
return;
}
// Use the cached value if its available.
//
// TODO: correct caching; update the call to setCachedValue to set the expiry
// to the value returned with the auth token.
//
// TODO: correct caching; enable the cache to be cleared.
$cached = $this->getCachedValue();
if (!is_null($cached)) {
$request->setHeader('Authorization', 'Bearer ' . $cached);
return;
}
// Fetch the auth token.
$auth_tokens = $this->fetcher->fetchAuthToken();
if (array_key_exists('access_token', $auth_tokens)) {
$request->setHeader('Authorization', 'Bearer ' . $auth_tokens['access_token']);
$this->setCachedValue($auth_tokens['access_token']);
}
}
示例3: onBefore
/**
* @param BeforeEvent $event
*/
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
$span = $this->guzzleMessageSpanFactory->fromOutgoingRequest($request);
$this->tracer->trace([$span]);
$request->addHeaders(['X-B3-SpanId' => (string) $span->getIdentifier(), 'X-B3-TraceId' => (string) $span->getTraceIdentifier(), 'X-B3-ParentSpanId' => (string) $span->getParentIdentifier(), 'X-B3-Flags' => $span->getDebug() ? '1' : '0']);
}
示例4: onBefore
/**
* Request before-send event handler.
*
* Adds the Authorization header if an access token was found.
*
* @param BeforeEvent $event Event received
*/
public function onBefore(BeforeEvent $event)
{
$this->checkTokenData();
if ($this->tokenData->accessToken) {
$this->accessTokenSigner->sign($event->getRequest(), $this->tokenData->accessToken);
}
}
示例5: onBefore
/**
* @param BeforeEvent $event
*/
public function onBefore(BeforeEvent $event)
{
if ($this->logger) {
$request = $event->getRequest();
$this->logger->info($this->formatMessage($request->getMethod(), $request->getUrl()), ['requestHeaders' => $request->getHeaders(), 'requestBody' => (string) $request->getBody()]);
}
}
示例6: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (isset(self::$purgeMethods[$request->getMethod()])) {
$this->storage->purge($request);
}
}
示例7: sign
/**
* Signs an API request using an authentication flow.
*
* @param BeforeEvent $event
*/
public function sign(BeforeEvent $event)
{
$request = $event->getRequest();
if ($request->getMethod() === 'GET') {
$this->authenticateGetRequest($request);
}
}
示例8: onBefore
/**
* @throws \OutOfBoundsException|\Exception
*/
public function onBefore(BeforeEvent $event)
{
if (!($item = array_shift($this->queue))) {
throw new \OutOfBoundsException('Mock queue is empty');
} elseif ($item instanceof RequestException) {
throw $item;
}
// Emulate reading a response body
$request = $event->getRequest();
if ($this->readBodies && $request->getBody()) {
while (!$request->getBody()->eof()) {
$request->getBody()->read(8096);
}
}
$saveTo = $event->getRequest()->getConfig()->get('save_to');
if (null !== $saveTo) {
$body = $item->getBody();
if (is_resource($saveTo)) {
fwrite($saveTo, $body);
} elseif (is_string($saveTo)) {
file_put_contents($saveTo, $body);
} elseif ($saveTo instanceof StreamInterface) {
$saveTo->write($body);
}
}
$event->intercept($item);
}
示例9: auth
public function auth(BeforeEvent $event, $name)
{
$cfg = $event->getRequest()->getConfig()['auth'];
$request = $event->getRequest();
if ($cfg == 'basic') {
call_user_func_array([$this, 'basic' . ucfirst($this->_class) . 'Auth'], [$request]);
}
}
示例10: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (!($response = $this->cache->fetch($request->__toString()))) {
return;
}
$event->intercept($response);
}
示例11: sign
public function sign(BeforeEvent $e)
{
$method = $e->getRequest()->getMethod();
if (preg_match('/post|put|delete/i', $method)) {
$e->getRequest()->setHeader('X-Insta-Forwarded-For', join('|', array($this->ip_address, hash_hmac('SHA256', $this->ip_address, $this->client_secret))));
}
// var_dump($e->getRequest()->getHeaders());
}
示例12: sign
/**
* Check if the requested authentication type matches and set the
* required X-Token and X-Signature headers
*
* @param BeforeEvent $event
*/
public function sign(BeforeEvent $event)
{
if ($event->getRequest()->getConfig()['auth'] == 'tevoauth') {
$request = $event->getRequest();
$request->setHeader('X-Token', $this->_apiToken);
$request->setHeader('X-Signature', $this->getSignature($request));
}
}
示例13: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
$this->context->signer()->sign(new Message($request));
$headers = new MessageHeaders($request);
$headers->set("Authorization", $headers->get("Signature"));
$request->removeHeader("Signature");
}
示例14: onRequestCreate
/**
* Add WSSE auth headers
*
* @param Event $event
*/
public function onRequestCreate(\GuzzleHttp\Event\BeforeEvent $event)
{
$request = $event->getRequest();
$nonce = call_user_func($this->noncer);
$created = date('r');
$digest = call_user_func($this->digester, $nonce, $created, $this->password);
$request->addHeaders(array("Authorization" => "WSSE profile=\"UsernameToken\"", "X-WSSE" => "UsernameToken Username=\"{$this->username}\", PasswordDigest=\"{$digest}\", Nonce=\"{$nonce}\", Created=\"{$created}\""));
}
示例15: onBefore
/**
* Updates the request query with the developer key if auth is set to simple.
*
* use Google\Auth\Subscriber\SimpleSubscriber;
* use GuzzleHttp\Client;
*
* $my_key = 'is not the same as yours';
* $subscriber = new SimpleSubscriber(['key' => $my_key]);
*
* $client = new Client([
* 'base_url' => 'https://www.googleapis.com/discovery/v1/',
* 'defaults' => ['auth' => 'simple']
* ]);
* $client->getEmitter()->attach($subscriber);
*
* $res = $client->get('drive/v2/rest');
*
* @param BeforeEvent $event
*/
public function onBefore(BeforeEvent $event)
{
// Requests using "auth"="simple" with the developer key.
$request = $event->getRequest();
if ($request->getConfig()['auth'] != 'simple') {
return;
}
$request->getQuery()->overwriteWith($this->config);
}