/** * Submit a request to the API. * * @param Requestable $request * @return object */ public function submit(RequestableInterface $request) { $this->applyDefaultOptions($request); $url = $this->options['host'] . $request->getPath(); $data = Packer::pack($request); return new Response($this->browser->submit($url, $data)); }
/** * @param string $endpoint * @param string $content * @param array $headers * @param array $files * @return Response */ public function send($endpoint, $content, array $headers = array(), array $files = array()) { // Make headers buzz friendly array_walk($headers, function (&$value, $key) { $value = sprintf('%s: %s', $key, $value); }); if ($files) { // HTTP query content parse_str($content, $fields); // Add files to request foreach ($files as $key => $items) { $fields[$key] = array(); foreach ($items as $name => $item) { $item = new FormUpload($item); if (!is_numeric($name)) { $item->setName($name); } $fields[$key] = $item; } } $response = $this->browser->submit($endpoint, $fields, RequestInterface::METHOD_POST, array_values($headers)); } else { // JSON content $response = $this->browser->post($endpoint, array_values($headers), $content); } return new Response($response->getStatusCode(), $response->getContent()); }
/** * {@inheritDoc} */ public function call(RequestInterface $request) { /** * @var Response $response */ $response = $this->browser->submit($this->getEndpoint(), $request->getParameters()); if ($response->isSuccessful()) { return $response->getContent(); } else { return false; } }
/** * Checks if the passed value is valid. * * @param mixed $value The value that should be validated * @param Constraint $constraint The constraint for the validation * * @api */ public function validate($value, Constraint $constraint) { $reCaptchaResponse = $this->request->request->get('g-recaptcha-response'); if (empty($reCaptchaResponse)) { $this->context->addViolation($constraint->message); return; } $response = $this->buzz->submit('https://www.google.com/recaptcha/api/siteverify', ['secret' => $this->secret, 'response' => $reCaptchaResponse, 'remoteip' => $this->request->getClientIp()]); $reCaptchaValidationResponse = json_decode($response->getContent()); if (true !== $reCaptchaValidationResponse->success) { $this->context->addViolation($constraint->message); } }
/** * Send request and generate response. * * @param Bool secure * * @throws UniversalAnalytics\Exception\InvalidRequestException * * @return Response */ public function send($secure = true) { $buzzBrowser = new Browser(); $buzzBrowser->setClient(new Curl()); $base = $secure ? $this->base_ssl : $this->base; $buzzResponse = $buzzBrowser->submit($base, $this->attributes, RequestInterface::METHOD_POST, array()); return new Response($buzzResponse); }
/** * {@inheritdoc} */ public function httpRequest($uri, array $body = [], array $headers = [], $method = 'POST') { try { $response = $this->httpTransporter->submit($uri, $body, $method, $headers); } catch (RequestException $e) { throw new TokenResponseException($e->getMessage() ? $e->getMessage() : 'Failed to request resource.'); } return $response->getContent(); }
/** * @When /^I submit "([^"]*)" data to "([^"]*)"$/ * @param string $pageUrl * @return void * @throws \Exception */ public function iSubmitDataTo($dataKey, $pageUrl) { $this->responseData = $this->responseDecodeException = null; $this->responseIsJson = false; if ($this->access_token) { $this->headers['Authorization'] = 'Bearer ' . $this->access_token; } $this->client->submit($this->processPageUrl($pageUrl), array($dataKey => $this->collectedData[$dataKey]), strtolower($this->requestMethod), $this->headers); $this->response = $this->client->getLastResponse(); }
/** * Returns raw response for request. * * @return string * @throws \Paybox\Exception\Http */ protected function getRawResponse() { /** @var \Buzz\Message\Response $result */ $uri = sprintf('%s://%s%s', $this->scheme, $this->host, $this->getRequestUrl()); $result = $this->browser->submit($uri, (new Sign($this->secret))->sign($uri, $this->toArray())); if ($result->isClientError() || $result->isServerError()) { if ($result->getStatusCode() >= 400) { throw new Http(sprintf('Paybox responded with code %d: %s', $result->getStatusCode(), $result->getContent()), $result->getStatusCode()); } } return $result->getContent(); }
/** * Send request and generate response * * @param Bool secure * @throws UniversalAnalytics\Exception\InvalidRequestException * @return Response */ public function send($secure = true) { $headers = array(); if (is_null($this->user_agent_string) === false) { $headers['User-Agent'] = $this->user_agent_string; } $buzzBrowser = new Browser(); $buzzBrowser->setClient(new Curl()); $base = $secure ? $this->base_ssl : $this->base; $buzzResponse = $buzzBrowser->submit($base, $this->attributes, RequestInterface::METHOD_POST, $headers); return new Response($buzzResponse); }
/** * Send an http request * * @param string $method The HTTP method * @param string $url The url to send the request to * @param array $parameters The parameters for the request (assoc array) * @param array $headers The headers for the request (assoc array) * * return mixed */ public function request($method, $url, array $parameters = null, array $headers = null) { // add query parameters to the url if needed if (isset($parameters['query']) && is_array($parameters['query'])) { $query = parse_url($url, PHP_URL_QUERY); $url .= ($query === null ? '?' : '&') . http_build_query($parameters['query']); } // make sure files are added as a form upload if ($method === 'POST') { foreach ($parameters as $key => $value) { if (is_string($value) && substr($value, 0, 1) == '@') { $value = ltrim($value, '@'); $parameters[$key] = new FormUpload($value); } } } // buzz requires us to send an array of parameters instead of null if (empty($parameters)) { $parameters = array(); } $response = $this->browser->submit($url, $parameters, $method, $headers); return json_decode($response->getContent(), true); }
/** * Perform an HTTP call * * $arguments is made of two arguments: * - first: route (Example: "{token}/ticket/list/{root}") * - second: parameters (Example: array("{root}" => "12", "key1" => "value2"), optionnal) * => {token}/ticket/list/12?key1=value2 * * @param string $method * @param array $arguments * @return array */ public function __call($method, $arguments) { // Check for arguments if (empty($arguments)) { throw new Exception("No arguments have been passed to ApiMapper::{$method}()"); } // Extracts arguments (Note $parameters and $fields are optionnal) $route = array_shift($arguments); $parameters = empty($arguments) ? array() : array_shift($arguments); $fields = empty($arguments) ? array() : array_shift($arguments); // Fill route placeholders, and append query fields $url = $this->buildUrl($route, $parameters); // Add post field parameter providers foreach ($this->postFieldsParameters as $fieldName => $field) { $field = $field->lookup($route); if ($field !== false) { $fields[$fieldName] = $field; } } // Load headers $headers = array(); foreach ($this->headerProviders as $headerProvider) { $header = $headerProvider->lookup($route); if ($header !== false) { $headers[] = $header; } } // Perform the call if (static::isSafeMethod($method)) { $response = $this->browser->call($url, $method, $headers); } else { $response = $this->browser->submit($url, $fields, $method, $headers); } // Parse the content $content = array("method" => strtoupper($method), "route" => $route, "url" => $url, "response" => $response, "parameters" => $parameters, "json" => json_decode($response->getContent(), true)); // Dispatch content to event listeners $this->dispatch($content); // Return the content return $content; }
protected function request($url, $data = array(), $method = 'POST', array $headers = array()) { if ($data instanceof Form) { $data = $data->toArray(); } $client = new Curl(); $client->setTimeout(100); $client->setVerifyPeer(FALSE); $browser = new Browser($client); $response = $browser->submit($url, $data, $method, $headers); return $response->getContent(); }
private function getAccessToken() { $response = $this->browser->submit(static::ACCESS_TOKEN_API_ENDPOINT, array('client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'scope' => 'http://api.microsofttranslator.com', 'grant_type' => 'client_credentials')); $data = json_decode($response->getContent()); return $data->access_token; }