本文整理汇总了PHP中Guzzle\Http\Client::getEventDispatcher方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::getEventDispatcher方法的具体用法?PHP Client::getEventDispatcher怎么用?PHP Client::getEventDispatcher使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Client
的用法示例。
在下文中一共展示了Client::getEventDispatcher方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addDebugOptionsToHttpClient
public static function addDebugOptionsToHttpClient(Client $guzzleClient)
{
$guzzleClient->setDefaultOption('debug', true);
$echoCb = function (Event $event) {
echo (string) $event['request'] . PHP_EOL;
echo (string) $event['response'] . PHP_EOL;
};
$guzzleClient->getEventDispatcher()->addListener('request.complete', $echoCb);
$guzzleClient->getEventDispatcher()->addListener('request.exception', $echoCb);
}
示例2: getGuzzle
protected function getGuzzle()
{
$this->historyPlugin = new HistoryPlugin();
$this->mockPlugin = new MockPlugin();
$this->mockPlugin->addResponse(new GuzzleResponse(200, null, '<html><body><p>Hi</p></body></html>'));
$guzzle = new GuzzleClient('', array('redirect.disable' => true));
$guzzle->getEventDispatcher()->addSubscriber($this->mockPlugin);
$guzzle->getEventDispatcher()->addSubscriber($this->historyPlugin);
return $guzzle;
}
示例3: shouldAuthenticateUsingAllGivenParameters
/**
* @test
* @dataProvider getAuthenticationFullData
*/
public function shouldAuthenticateUsingAllGivenParameters($site, $login, $password)
{
$client = new GuzzleClient();
$listeners = $client->getEventDispatcher()->getListeners('request.before_send');
$this->assertCount(1, $listeners);
$httpClient = new HttpClient(array(), $client);
$httpClient->authenticate($site, $login, $password);
$listeners = $client->getEventDispatcher()->getListeners('request.before_send');
$this->assertCount(2, $listeners);
$authListener = $listeners[1][0];
$this->assertInstanceOf('Eloqua\\HttpClient\\Listener\\AuthListener', $authListener);
}
示例4: setUp
public function setUp()
{
$this->guzzleClient = new Client('http://[::1]:8999');
global $argv;
if (in_array('--debug', $argv, true)) {
$echoCb = function (Event $event) {
echo (string) $event['request'] . PHP_EOL;
echo (string) $event['response'] . PHP_EOL;
};
$this->guzzleClient->getEventDispatcher()->addListener('request.complete', $echoCb);
$this->guzzleClient->getEventDispatcher()->addListener('request.exception', $echoCb);
}
}
示例5: getClient
protected function getClient($response, $code = 200, $throwCurlException = false)
{
$plugin = new MockPlugin();
$plugin->addResponse(new Response($code, null, $response));
$clientHttp = new GuzzleClient('http://my.domain.tld/api/v1');
$clientHttp->getEventDispatcher()->addSubscriber($plugin);
if ($throwCurlException) {
$clientHttp->getEventDispatcher()->addListener('request.before_send', function (\Guzzle\Common\Event $event) {
throw new \Guzzle\Http\Exception\CurlException();
});
}
$logger = new Logger('tests');
$logger->pushHandler(new NullHandler());
return new Client('123456', '654321', new GuzzleAdapter($clientHttp), $logger);
}
示例6: createClient
private function createClient()
{
$client = new Client($this->getBaseUrl());
$client->getEventDispatcher()->addListener('request.error', static function (Event $event) {
$event->stopPropagation();
});
return $client;
}
示例7: executeRequest
/**
* @param Url $url
* @return \Guzzle\Http\Message\Response
*/
protected function executeRequest(Url $url)
{
$guzzle = new GuzzleClient();
$guzzle->getEventDispatcher()->addListener('request.error', function (Event $event) {
if ($event['response']->getStatusCode() != 200) {
$event->stopPropagation();
}
});
return $guzzle->createRequest('GET', $url)->send();
}
示例8: setAuthHeader
/**
* Sets the authorization header provided by the token
*
* @param Array token Array with token info
* @return Void
*/
protected function setAuthHeader(array $token)
{
$this->configs = array_merge($this->configs, $token);
if (!empty($this->configs['access_token'])) {
$authorization = sprintf('Bearer %s', $this->configs['access_token']);
$this->client->getEventDispatcher()->addListener('request.before_send', function (Event $event) use($authorization) {
$event['request']->addHeader('Authorization', $authorization);
});
}
}
示例9: test_cache_configuration_config_present
/**
* Sanity check to make sure that OoyalaCache is a subscriber.
*
* The only reason this is needed is because the plugin might be disabled
* because of configuration, so it's good to check when some configuration
* is present that the plugin actually does not remove itself as a subscriber.
*/
public function test_cache_configuration_config_present()
{
$client = new Client('http://test.com', array('ooyala.cache' => true));
$plugin = new OoyalaCache();
$className = get_class($plugin);
$client->addSubscriber($plugin);
$client->getEventDispatcher()->dispatch(OoyalaClient::EVENT_INITIALIZED, new Event(array('client' => $client)));
$subscribedEvents = array_keys(OoyalaCache::getSubscribedEvents());
foreach ($client->getEventDispatcher()->getListeners() as $eventName => $listeners) {
if (!in_array($eventName, $subscribedEvents)) {
continue;
}
$actual = array();
foreach ($listeners as $listener) {
$actual[] = get_class($listener[0]);
}
$this->assertTrue(in_array($className, $actual));
}
}
示例10: testAddMixpanelAuthentication
public function testAddMixpanelAuthentication()
{
$plugin = new MixGuzzleAuthPlugin('key', 'secret');
$client = new Client('http://test.com/');
$client->getEventDispatcher()->addSubscriber($plugin);
$request = $client->get('/');
$event = new Event(array('request' => $request, 'timestamp' => 123456789));
$params = $plugin->onRequestBeforeSend($event);
$this->assertArrayHasKey('sig', $params);
}
示例11: create
/**
* Creates the GuzzleTeleporter
*
* @return GuzzleTeleporter
*/
public static function create()
{
$client = new Client();
$client->getEventDispatcher()->addListener('request.error', function (Event $event) {
// override guzzle default behavior of throwing exceptions
// when 4xx & 5xx responses are encountered
$event->stopPropagation();
}, -254);
$client->addSubscriber(BackoffPlugin::getExponentialBackoff(5, array(500, 502, 503, 408)));
return new static($client);
}
示例12: testAddPsrLoggerToClient
public function testAddPsrLoggerToClient()
{
$client = new Client();
$listenersBefore = $client->getEventDispatcher()->getListeners('request.sent');
$logger = $this->getMockForAbstractClass('Psr\\Log\\LoggerInterface');
$plugin = GuzzleClientHelper::addPsrLoggerToClient($client, $logger);
$this->assertTrue($client->getEventDispatcher()->hasListeners('request.sent'));
$expectedListenersCount = count($listenersBefore) + 1;
$this->assertCount($expectedListenersCount, $client->getEventDispatcher()->getListeners('request.sent'));
$this->assertContains([$plugin, 'onRequestSent'], $client->getEventDispatcher()->getListeners('request.sent'));
}
示例13: testAddsDigestAuthentication
public function testAddsDigestAuthentication()
{
$plugin = new CurlAuthPlugin('julian', 'test', CURLAUTH_DIGEST);
$client = new Client('http://www.test.com/');
$client->getEventDispatcher()->addSubscriber($plugin);
$request = $client->get('/');
$this->assertEquals('julian', $request->getUsername());
$this->assertEquals('test', $request->getPassword());
$this->assertEquals('julian:test', $request->getCurlOptions()->get(CURLOPT_USERPWD));
$this->assertEquals(CURLAUTH_DIGEST, $request->getCurlOptions()->get(CURLOPT_HTTPAUTH));
}
示例14: testAddsWsseAuthenticationWithCustomDigest
public function testAddsWsseAuthenticationWithCustomDigest()
{
$plugin = new WsseAuthPlugin('michael', 'test', function ($nonce, $created, $password) {
return 'this_digest_wont_work';
});
$client = new Client('http://www.test.com/');
$client->getEventDispatcher()->addSubscriber($plugin);
$request = $client->get('/');
$headers = $request->getHeaders();
$this->assertTrue(false !== $headers->hasKey('Authorization'));
$this->assertTrue(false !== $headers->hasKey('X-WSSE'));
$this->assertTrue(false !== strpos(array_shift($headers->get('X-WSSE')), 'this_digest_wont_work'));
}
示例15: testNotLimited
public function testNotLimited()
{
// Create a script to return a 429 response like Desk.com
$this->getServer()->flush();
$this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata"));
$plugin = new BackoffPlugin($this->strategy());
$client = new Client($this->getServer()->getUrl());
$client->getEventDispatcher()->addSubscriber($plugin);
$request = $client->get();
$request->send();
// Make sure it eventually completed successfully
$this->assertEquals(200, $request->getResponse()->getStatusCode());
$this->assertEquals('data', $request->getResponse()->getBody(true));
// Check that two requests were made to retry this request
$this->assertEquals(1, count($this->getServer()->getReceivedRequests(false)));
$this->assertEquals(0, $request->getParams()->get(BackoffPlugin::RETRY_PARAM));
}