本文整理匯總了PHP中Buzz\Browser::send方法的典型用法代碼示例。如果您正苦於以下問題:PHP Browser::send方法的具體用法?PHP Browser::send怎麽用?PHP Browser::send使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Buzz\Browser
的用法示例。
在下文中一共展示了Browser::send方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: 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);
}
示例2: request
/**
* @param Request $request
* @return Response
*/
public function request(Request $request)
{
$buzzRequest = $this->prepareRequest($request);
/** @var BuzzResponse $buzzResponse */
$buzzResponse = $this->browser->send($buzzRequest);
return new Response((string) $buzzResponse->getProtocolVersion(), $buzzResponse->getStatusCode(), $buzzResponse->getReasonPhrase(), HeaderConverter::convertRawToAssociative($buzzResponse->getHeaders()), $buzzResponse->getContent());
}
示例3: execute
/**
* Executes a http request.
*
* @param Request $request The http request.
*
* @return Response The http response.
*/
public function execute(Request $request)
{
$method = $request->getMethod();
$endpoint = $request->getEndpoint();
$params = $request->getParams();
$headers = $request->getHeaders();
try {
if ($method === 'GET') {
$buzzResponse = $this->client->call($endpoint . '?' . http_build_query($params), $method, $headers, array());
} else {
$buzzRequest = new FormRequest();
$buzzRequest->fromUrl($endpoint);
$buzzRequest->setMethod($method);
$buzzRequest->setHeaders($headers);
foreach ($params as $key => $value) {
if ($value instanceof Image) {
$value = new FormUpload($value->getData());
}
$buzzRequest->setField($key, $value);
}
$buzzResponse = new BuzzResponse();
$this->client->send($buzzRequest, $buzzResponse);
}
} catch (RequestException $e) {
throw new Exception($e->getMessage());
}
return static::convertResponse($request, $buzzResponse);
}
示例4: 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()));
}
示例5: sendRequest
/**
* @param FormRequest $request
* @return ApiData
*/
private function sendRequest(FormRequest $request)
{
/**
* @var Response $response
*/
$response = $this->browser->send($request);
return new ApiData($response, $this->reader->read($response));
}
示例6: fetch
/**
* Fetch from the API
* @param string $endpoint Fetch from the endpoint
* @param array $params Any parameters
* @return \stdClass|\stdClass[]
*/
public static function fetch($endpoint, array $params = [])
{
$client = self::getClient();
$browser = new Browser($client);
$method = Request::METHOD_GET;
foreach (self::$postEndpoints as $check) {
if (preg_match("/^{$check}\$/", $endpoint)) {
$method = Request::METHOD_POST;
}
}
$request = new FormRequest($method, $endpoint, self::BASE_URL);
$request->setFields($params);
$response = new Response();
$listener = new JsonListener();
$browser->addListener($listener);
$response = $browser->send($request, $response);
return $response->getContent();
}
示例7: getMedia
/**
* @param $referenceFull
*
* @return Response
*
* @throws MediaStorageClientApiException
*/
public function getMedia($referenceFull)
{
if (empty($referenceFull)) {
throw new MediaStorageClientApiException('Param referenceFull is empty');
}
try {
$request = new FormRequest();
$request->setMethod(FormRequest::METHOD_GET);
$request->setHost($this->getApiUrlConfig('base_url'));
$url = $this->getApiUrlConfig('get_media_by_reference_full_url') . '/' . ltrim($referenceFull, '/');
$request->setResource($url);
$this->logger->debug('Send ' . $this->getApiUrlConfig('base_url') . $url);
/** @var Response $response */
$response = $this->client->send($request, null);
$this->logger->debug('Response: ' . $response->getStatusCode() . ' ' . substr($response->getContent(), 0, 300));
} catch (\Exception $e) {
throw new MediaStorageClientApiException($e->getMessage());
}
return $response;
}
示例8: getResult
/**
* Возвращает результаты запроса.
*
* @return mixed
* @throws \Api\Exceptions\Exception
*/
public function getResult()
{
$log = ['time' => microtime(true), 'uri' => sprintf('%s?%s', $this->uri, http_build_query($this->queryParameters)), 'code' => 200];
$uri = sprintf('%s://%s/%s?%s', $this->api->getScheme(), $this->api->getDomain(), $this->uri, http_build_query(array_merge($this->queryParameters, ['appId' => $this->api->getAppId(), 'appKey' => $this->api->getAppKey()])));
$browser = new Browser(new Curl());
$request = new FormRequest($this->method, $uri);
if ($this->method == RequestInterface::METHOD_POST && !empty($this->postParameters)) {
foreach ($this->postParameters as $key => $value) {
$request->setField($key, $value);
}
}
/** @var \Buzz\Message\Response $result */
$result = $browser->send($request);
$exception = null;
if ($result->isClientError() || $result->isServerError()) {
switch ($result->getStatusCode()) {
case 404:
$exception = new Http(sprintf('API returned 404 on request to «%s»: %s', $this->uri, $result->getContent()), 404);
break;
default:
$exception = new Http(sprintf('API returned 500 on request to «%s»: %s', $this->uri, $result->getContent()), 500);
break;
}
}
if (null !== $exception) {
$log['time'] = microtime(true) - $log['time'];
$log['code'] = $exception->getCode();
$this->api->appendLog($log);
throw $exception;
}
$json = json_decode($result->getContent(), true);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new Json(sprintf('%s on %s', $this->jsonError($error), $this->uri));
}
$log['time'] = microtime(true) - $log['time'];
$this->api->appendLog($log);
return $json;
}