本文整理匯總了PHP中Buzz\Browser::getClient方法的典型用法代碼示例。如果您正苦於以下問題:PHP Browser::getClient方法的具體用法?PHP Browser::getClient怎麽用?PHP Browser::getClient使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Buzz\Browser
的用法示例。
在下文中一共展示了Browser::getClient方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __construct
/**
* @param $timeout
*/
public function __construct($timeout, $logger)
{
$this->browser = new Browser(new Curl());
$this->browser->getClient()->setVerifyPeer(false);
$this->browser->getClient()->setTimeout($timeout);
$this->logger = $logger;
}
示例2: send
/**
* @param array $data
*
* @return array|string|null
*
* @throws \Exception
*/
public function send($data = null)
{
$request = new FormRequest($this->method, $this->resource, $this->host);
if ($data) {
$request->addFields($data);
}
try {
$this->logger->addDebug('Request: ' . $request->getUrl());
/** @var Buzz\Message\Response $response */
$response = $this->client->send($request);
$this->logger->addDebug('Response: ' . $response->getStatusCode() . ' ' . substr($response->getContent(), 0, 300) . PHP_EOL . var_export($this->client->getClient()->getInfo(), true));
} catch (\Exception $e) {
switch ($e->getCode()) {
case 28:
$code = 504;
break;
default:
$code = $e->getCode() >= 100 ? $e->getCode() : 500;
}
$this->logger->addCritical(PHP_EOL . __METHOD__ . sprintf('[%s/%s] %s', $e->getCode(), $code, $e->getMessage()));
throw new WebGateException($e->getMessage(), $code, $e);
}
if ($response->getStatusCode() != 200) {
throw new WebGateException(json_decode($response->getContent(), true), $response->getStatusCode());
}
return json_decode($response->getContent(), true);
}
示例3: send
/**
* Sends the data to the given registration IDs via the GCM server
*
* @param \RMS\PushNotificationsBundle\Message\MessageInterface $message
* @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException
* @return bool
*/
public function send(MessageInterface $message)
{
if (!$message instanceof AndroidMessage) {
throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by GCM", get_class($message)));
}
if (!$message->isGCM()) {
throw new InvalidMessageTypeException("Non-GCM messages not supported by the Android GCM sender");
}
$headers = array("Authorization: key=" . $this->apiKey, "Content-Type: application/json");
$data = array_merge($message->getGCMOptions(), array("data" => $message->getData()));
// Chunk number of registration IDs according to the maximum allowed by GCM
$chunks = array_chunk($message->getGCMIdentifiers(), $this->registrationIdMaxCount);
// Perform the calls (in parallel)
$this->responses = array();
foreach ($chunks as $registrationIDs) {
$data["registration_ids"] = $registrationIDs;
$this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data));
}
// If we're using multiple concurrent connections via MultiCurl
// then we should flush all requests
if ($this->browser->getClient() instanceof MultiCurl) {
$this->browser->getClient()->flush();
}
// Determine success
foreach ($this->responses as $response) {
$message = json_decode($response->getContent());
if ($message === null || $message->success == 0 || $message->failure > 0) {
return false;
}
}
return true;
}
示例4: __construct
/**
* Class constructor.
*
* @param $apiKey
* @param null $apiUrl
*/
public function __construct($apiKey, $apiUrl = null)
{
$this->apiKey = $apiKey;
if ($apiUrl) {
$this->apiUrl = $apiUrl;
}
$this->browser = new Browser(new MultiCurl());
$this->browser->getClient()->setVerifyPeer(false);
}
示例5: __construct
/**
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = array_merge(static::getDefaultConfigs(), $options);
$buzzClassName = $this->options['buzz.class'];
$this->buzz = new $buzzClassName();
$this->servers = $this->options['replay.http_client.servers'];
// Increase timeout
$this->buzz->getClient()->setTimeout($this->options['buzz.timeout']);
}
示例6: execute
/**
* Execute an Api request
*
* @param string $endpoint
* @param array $payload
* @param array $options
* @return mixed
*/
public function execute($endpoint, array $payload, array $options = [])
{
$endpoint = $this->baseEndpoint . $endpoint;
// set a timeout if applicable
if (isset($options['timeout'])) {
$this->transport->getClient()->setTimeout((int) $options['timeout']);
} else {
if (isset($this->timeout)) {
$this->transport->getClient()->setTimeout((int) $this->timeout);
}
}
$response = $this->transport->post($endpoint, $this->getHeaders(), json_encode(['meta' => ['when' => date('Y-m-d H:i:s'), 'server' => gethostname()], 'data' => $payload]));
return json_decode($response->getContent(), true);
}
示例7: __construct
/**
* Instantiated a new http client
*
* @param array $options Http client options
* @param null|Browser $browser Buzz client
*/
public function __construct(array $options = array(), Browser $browser = null)
{
$this->options = array_merge($this->options, $options);
$this->browser = $browser ?: new Browser(new Curl());
$this->browser->getClient()->setTimeout($this->options['timeout']);
$this->browser->getClient()->setVerifyPeer(false);
if (null !== $this->options['login'] || null !== $this->options['token']) {
if (null !== $this->options['token']) {
$options = array($this->options['token']);
} else {
$options = array($this->options['login'], $this->options['password']);
}
$this->browser->addListener(new AuthListener($this->options['auth_method'], $options));
}
}
示例8: flush
/**
* Flush notifications. Triggers the requests.
*
* @return array|bool If there are no errors, return true.
* If there were no notifications in the queue, return false.
* Else return an array of information for each notification sent (success, statusCode, headers, content)
*
* @throws \ErrorException
*/
public function flush()
{
if (empty($this->notifications)) {
return false;
}
// for each endpoint server type
$responses = $this->prepareAndSend($this->notifications);
// if multi curl, flush
if ($this->browser->getClient() instanceof MultiCurl) {
/** @var MultiCurl $multiCurl */
$multiCurl = $this->browser->getClient();
$multiCurl->flush();
}
/** @var Response|null $response */
$return = array();
$completeSuccess = true;
foreach ($responses as $i => $response) {
if (!isset($response)) {
$return[] = array('success' => false, 'endpoint' => $this->notifications[$i]->getEndpoint());
$completeSuccess = false;
} elseif (!$response->isSuccessful()) {
$return[] = array('success' => false, 'endpoint' => $this->notifications[$i]->getEndpoint(), 'statusCode' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'content' => $response->getContent(), 'expired' => in_array($response->getStatusCode(), array(400, 404, 410)));
$completeSuccess = false;
} else {
$return[] = array('success' => true);
}
}
// reset queue
$this->notifications = null;
return $completeSuccess ? true : $return;
}
示例9: post
/**
* @param string $url
* The URL to POST to
* @param SiteDocument $site
* The site being posted to
* @param array $params
* An array of keys and values to be posted
*
* @return mixed
* The content of the response
*
* @throws WardenRequestException
* If any error occurs
*/
public function post($url, SiteDocument $site, array $params = array())
{
try {
$this->setClientTimeout($this->connectionTimeout);
// Don't verify SSL certificate.
// @TODO make this optional
$this->buzz->getClient()->setVerifyPeer(FALSE);
if ($site->getAuthUser() && $site->getAuthPass()) {
$headers = array(sprintf('Authorization: Basic %s', base64_encode($site->getAuthUser() . ':' . $site->getAuthPass())));
$this->setConnectionHeaders($headers);
}
$params['token'] = $this->sslEncryptionService->generateRequestToken();
$content = http_build_query($params);
/** @var \Buzz\Message\Response $response */
$response = $this->buzz->post($url, $this->connectionHeaders, $content);
if (!$response->isSuccessful()) {
$this->logger->addError("Unable to request data from {$url}\nStatus code: " . $response->getStatusCode() . "\nHeaders: " . print_r($response->getHeaders(), TRUE));
throw new WardenRequestException("Unable to request data from {$url}. Check log for details.");
}
$site->setLastSuccessfulRequest();
$this->siteManager->updateDocument();
return $response->getContent();
} catch (ClientException $clientException) {
throw new WardenRequestException($clientException->getMessage());
}
}
示例10: checkAnswer
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @param string $remoteIp
* @param string $challenge
* @param string $response
* @throws Exception
* @return boolean
*/
public function checkAnswer($remoteIp, $challenge, $response)
{
$headers = array('Connection' => 'close');
$content = array('private_key' => $this->privateKey, 'session_token' => $response, 'simple_mode' => 1);
$browser = new Browser();
$browser->getClient()->setVerifyPeer(false);
try {
/** @var Response $resp */
$resp = $browser->post(self::VERIFY_SERVER, $headers, http_build_query($content));
} catch (Exception $e) {
throw new CaptchaException('Failed to send captcha', 500, $e);
}
if (!$resp->isSuccessful()) {
throw new CaptchaException('Error: ' . $resp->getStatusCode());
}
$answer = $resp->getContent();
if (!$answer) {
throw new CaptchaException('Error: Invalid captcha response');
}
$success = isset($answer) && filter_var($answer, FILTER_VALIDATE_INT);
if (!$success) {
return new CaptchaResponse($success, 'Invalid captcha');
}
return new CaptchaResponse($success);
}
示例11: checkAnswer
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @param string $remoteIp
* @param string $challenge
* @param string $response
* @throws Exception
* @return boolean
*/
public function checkAnswer($remoteIp, $challenge, $response)
{
if (empty($remoteIp)) {
throw new CaptchaException('RemoteIp missing');
}
$headers = array('Connection' => 'close');
$content = array('secret' => $this->privateKey, 'remoteip' => $remoteIp, 'response' => $response);
$browser = new Browser();
$browser->getClient()->setVerifyPeer(false);
try {
/** @var Response $resp */
$resp = $browser->post(self::VERIFY_SERVER, $headers, http_build_query($content));
} catch (Exception $e) {
throw new CaptchaException('Failed to check captcha', 500, $e);
}
if (!$resp->isSuccessful()) {
throw new CaptchaException('Error: ' . $resp->getStatusCode());
}
$answer = json_decode($resp->getContent());
if (!$answer) {
throw new CaptchaException('Error: Invalid captcha response');
}
$success = isset($answer->success) && filter_var($answer->success, FILTER_VALIDATE_BOOLEAN);
if (!$success) {
return new CaptchaResponse($success, 'Invalid captcha');
}
return new CaptchaResponse($success);
}
示例12: sendInternalRequest
/**
* {@inheritdoc}
*/
protected function sendInternalRequest(InternalRequestInterface $internalRequest)
{
$this->browser->getClient()->setTimeout($this->getConfiguration()->getTimeout());
$this->browser->getClient()->setMaxRedirects(0);
$request = $this->browser->getMessageFactory()->createRequest($internalRequest->getMethod(), $uri = (string) $internalRequest->getUri());
$request->setProtocolVersion($internalRequest->getProtocolVersion());
$request->setHeaders($this->prepareHeaders($internalRequest, false));
$data = $this->browser->getClient() instanceof AbstractCurl ? $this->prepareContent($internalRequest) : $this->prepareBody($internalRequest);
$request->setContent($data);
try {
$response = $this->browser->send($request);
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
}
return $this->getConfiguration()->getMessageFactory()->createResponse($response->getStatusCode(), (string) $response->getProtocolVersion(), HeadersNormalizer::normalize($response->getHeaders()), BodyNormalizer::normalize($response->getContent(), $internalRequest->getMethod()));
}
示例13: __construct
/**
* Mockable constructor.
* @param Browser $browser Buzz instance
* @param array $options
*/
public function __construct(Browser $browser, array $options)
{
if ($browser->getClient() instanceof FileGetContents) {
throw new \InvalidArgumentException('The FileGetContents client is known not to work with this library. Please instantiate the Browser with an instance of \\Buzz\\Client\\Curl');
}
$this->browser = $browser;
$this->options = $options;
}
示例14: sendRequest
/**
* Sends a request.
*
* @param string $url The url.
* @param string $method The http method.
* @param array $headers The header.
* @param array $content The content.
*
* @throws \Widop\HttpAdapter\HttpAdapterException If an error occured.
*
* @return \Widop\HttpAdapter\HttpResponse The response.
*/
private function sendRequest($url, $method, array $headers = array(), array $content = array())
{
$this->browser->getClient()->setMaxRedirects($this->getMaxRedirects());
try {
$response = $this->browser->call($url, $method, $headers, $content);
} catch (\Exception $e) {
throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage());
}
return $this->createResponse($response->getStatusCode(), $url, $response->getHeaders(), $response->getContent());
}
示例15: setBrowser
/**
* @param Browser $browser
*/
public function setBrowser(Browser $browser)
{
$client = $browser->getClient();
$client->setVerifyPeer(false);
$client->setTimeout(30);
if ($client instanceof \Buzz\Client\Curl) {
$client->setOption(CURLOPT_USERAGENT, 'KnpBundles.com Bot');
}
$this->browser = $browser;
}