Beispiel #1
0
 /**
  *
  * @param string $path Path (will be appended to the Base URL)
  * @param array $params
  * @param mixed $method
  * @return \SimpleXMLElement
  */
 protected function request($path, array $params = null, $method = RequestInterface::METHOD_GET)
 {
     // Full URL
     $fullUrl = new Url($this->baseUrl . $path);
     // Additional headers
     $headers = array('User-Agent: ' . $this->getUserAgentString());
     // Got data?
     if ($method === RequestInterface::METHOD_GET) {
         if ($params != null && count($params) > 0) {
             $fullUrl = new Url($this->baseUrl . $path . '?' . http_build_query($params));
         }
         $response = $this->browser->get($fullUrl, $headers);
     } else {
         $response = $this->browser->call($fullUrl, $method, $headers, $params);
     }
     // Convert XML
     $xml = @simplexml_load_string($response->getContent());
     // Succesful?
     if ($xml === false) {
         throw new MollieException('Server did not respond with valid XML.');
     }
     // Error?
     if ($xml->item != null && (string) $xml->item['type'] == 'error') {
         throw new MollieException((string) $xml->item->message, intval($xml->item->errorcode));
     }
     return $xml;
 }
Beispiel #2
0
 /**
  * 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);
 }
Beispiel #3
0
 /**
  * Make a request against the FatSecret APIs.
  * 
  * @param String $base_url
  * @param array  $params
  * @param string $method
  *
  * @return \Buzz\Message\MessageInterface
  */
 public function send($base_url, array $params = [], $method = 'GET')
 {
     if (!$this->isInitialized()) {
         throw new \ErrorException("The consumer key and secret must be set first.");
     }
     $params = $this->parseParams($params);
     $signature = $this->getSignature($base_url, $params, $method);
     $params = array_merge($params, ['oauth_signature' => $signature]);
     $api_url = $this->getAPIEndpoint($base_url, $method, $params);
     return $this->client->call($api_url, $method, [], $params);
 }
 /**
  * {@inheritDoc}
  */
 public function getContent($url, $method = 'GET', array $headers = array(), $data = array())
 {
     if (is_array($data)) {
         $data = $this->encodePostData($data);
     }
     try {
         $response = $this->browser->call($url, $method, $headers, $data);
     } catch (\Exception $e) {
         return null;
     }
     return $response ? $response->getContent() : null;
 }
Beispiel #5
0
 /**
  * @todo: should also resolve jsonrpc and jsonrpcs schemas.
  *
  * {@inheritdoc}
  */
 public function call(Identity $identity, Request $request)
 {
     $url = $identity->getUrl(['https', 'http']);
     $headers = $this->getEncodedHeaders();
     $body = $this->getEncodedBody($request);
     /** @var HttpResponse $response */
     $response = $this->buzz->call($url, 'POST', $headers, $body);
     return new Promise(function () use($response) {
         $result = $this->getDecodedBody($response->getContent());
         return new Response($result);
     });
 }
 /**
  * {@inheritdoc}
  */
 public function call(LocationInterface $location, Request $request)
 {
     $headers = $this->getEncodedHeaders($location, $request);
     $body = $this->getEncodedBody($request);
     $url = $this->getRequestUrl($location);
     /** @var HttpResponse $response */
     $response = $this->buzz->call($url, 'POST', $headers, $body);
     return new Response\Promise(function () use($response) {
         $result = $this->getDecodedBody($response->getContent());
         $headers = $this->getDecodedHeaders($response->getHeaders());
         return new Response($result, $headers);
     });
 }
Beispiel #7
0
 /**
  * Prepare the curl request
  *
  * @param string $apiCall the API call function
  * @param array $payload Parameters
  * @return array
  */
 public function request($method, $apiCall, $payload = [])
 {
     $url = rtrim($this->baseUrl . $apiCall, '/?');
     $curl = $this->prepareCurl();
     $browser = new Browser($curl);
     $headers = ["Authorization" => "Bearer " . $this->token, "Content-type" => "application/json"];
     $response = $browser->call($url, $method, $headers, json_encode($payload));
     return $response->getContent();
 }
Beispiel #8
0
 /**
  * @When /^I request "([^"]*)"$/
  * @param  string     $pageUrl
  * @return void
  * @throws \Exception
  */
 public function iRequest($pageUrl)
 {
     $this->responseData = $this->responseDecodeException = null;
     $this->responseIsJson = false;
     if ($this->access_token) {
         $this->headers['Authorization'] = 'Bearer ' . $this->access_token;
     }
     $this->client->call($this->processPageUrl($pageUrl), strtolower($this->requestMethod), $this->headers);
     $this->response = $this->client->getLastResponse();
 }
Beispiel #9
0
 /**
  * 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());
 }
Beispiel #10
0
 /**
  * Purges all content elements currently in cache.
  *
  * @return void
  */
 public function purgeAll()
 {
     foreach ($this->purgeServers as $server) {
         $this->prepareUserAuth($server);
         $this->httpBrowser->call($server, 'PURGE', array('X-Location-Id' => '*'));
         $client = $this->httpBrowser->getClient();
         if ($client instanceof BatchClientInterface) {
             $client->flush();
         }
     }
 }
 public function call($api_method, $http_method = RequestInterface::METHOD_GET, array $query_string = array(), $headers = array(), $content = '')
 {
     $url = sprintf('%s/%s/%s?%s', $this->api_endpoints[array_rand($this->api_endpoints)], self::API_PATH, $api_method, http_build_query($query_string));
     $response = $this->client->call($url, $http_method, $headers, $content);
     if ($response->isSuccessful()) {
         $this->hasErrors($response->getContent());
         return $response->getContent();
     } else {
         return $this->handleException($response);
     }
 }
 /**
  * @param \MatthiasNoback\MicrosoftTranslator\ApiCall\ApiCallInterface $apiCall
  * @return mixed
  */
 private function call(ApiCall\ApiCallInterface $apiCall)
 {
     $url = $apiCall->getUrl();
     $method = $apiCall->getHttpMethod();
     $headers = array('Authorization: Bearer ' . $this->getAccessToken(), 'Content-Type: text/xml');
     $content = $apiCall->getRequestContent();
     try {
         $response = $this->browser->call($url, $method, $headers, $content);
     } catch (\Exception $previous) {
         throw new RequestFailedException(sprintf('Request failed: %s', $previous->getMessage()), null, $previous);
     }
     if (!$response->isSuccessful()) {
         throw new RequestFailedException(sprintf('API call was not successful, %d: %s', $response->getStatusCode(), $response->getReasonPhrase()));
     }
     /* @var $response \Buzz\Message\Response */
     $responseContent = $response->getContent();
     return $apiCall->parseResponse($responseContent);
 }
Beispiel #13
0
 /**
  * Sends a request.
  *
  * @param string $url     The URL to call
  * @param string $method  The request method to use
  * @param array  $headers An array of request headers
  * @param string $content The request content
  * @param int $cacheLifetime serverside cache lifetime
  *
  * @return MessageInterface The response object
  */
 public function call($url, $method, $headers = array(), $content = '', $cacheLifetime = null)
 {
     if ($this->cacher == null) {
         return parent::call($url, $method, $headers, $content, $cacheLifetime);
     }
     $request = $this->factory->createRequest($method);
     if (!$url instanceof Url) {
         $url = new Url($url);
     }
     $url->applyToRequest($request);
     $request->addHeaders($headers);
     $request->setContent($content);
     $this->data = $this->cacher->retrieveCachedResponse($request);
     if (!$this->data) {
         $this->fromCache = false;
         $this->send($request);
         $this->data = array('request' => parent::getLastRequest(), 'response' => parent::getLastResponse());
         $this->cacher->cacheResponse($this->data['request'], $this->data['response'], $cacheLifetime);
     }
     return $this->data['response'];
 }
Beispiel #14
0
 /**
  * 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;
 }
Beispiel #15
0
 /**
  * DoRequest
  * @param string $url
  * @param array $params
  * @param string $httpMethod
  * @param array $options
  * @return array
  */
 public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
 {
     if ($this->options['login']) {
         switch ($this->options['auth_method']) {
             case Client::AUTH_HTTP_PASSWORD:
                 $this->browser->getClient()->setOption(CURLOPT_USERPWD, $this->options['login '] . ':' . $this->options['secret']);
                 break;
             case Client::AUTH_HTTP_TOKEN:
                 $this->browser->getClient()->setOption(CURLOPT_USERPWD, $this->options['login'] . '/token:' . $this->options['secret']);
                 break;
             case Client::AUTH_URL_TOKEN:
             default:
                 $parameters = array_merge(array('login' => $this->options['login'], 'token' => $this->options['secret']), $parameters);
                 break;
         }
     }
     if (!empty($parameters)) {
         $queryString = utf8_encode(http_build_query($parameters, '', '&'));
         if ('GET' === $httpMethod) {
             $url .= '?' . $queryString;
         } else {
             $this->browser->getClient()->setOption(CURLOPT_POST, true);
             $this->browser->getClient()->setOption(CURLOPT_POSTFIELDS, $queryString);
         }
     }
     $this->browser->getClient()->setOption(CURLOPT_URL, $url);
     $this->browser->getClient()->setOption(CURLOPT_PORT, $this->options['http_port']);
     $this->browser->getClient()->setOption(CURLOPT_USERAGENT, $this->options['user_agent']);
     $this->browser->getClient()->setOption(CURLOPT_FOLLOWLOCATION, true);
     $this->browser->getClient()->setOption(CURLOPT_RETURNTRANSFER, true);
     $this->browser->getClient()->setOption(CURLOPT_SSL_VERIFYPEER, false);
     $this->browser->getClient()->setOption(CURLOPT_TIMEOUT, $this->options['timeout']);
     $response = $this->browser->call($url, $httpMethod, $this->headers, json_encode($parameters));
     $this->checkApiLimit($response);
     return array('response' => $response->getContent(), 'headers' => $response->getHeaders(), 'errorNumber' => '', 'errorMessage' => '');
 }
 /**
  * Sends HTTP request to specific relative URL.
  *
  * @param string $method request method
  * @param string $url    relative url
  *
  * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)"$/
  */
 public function iSendARequest($method, $url)
 {
     $url = $this->baseUrl . '/' . ltrim($this->replacePlaceHolder($url), '/');
     $this->browser->call($url, $method, $this->getHeaders());
 }
Beispiel #17
0
 /**
  * Send a request to the server, receive a response
  *
  * @param  string   $url           Request url
  * @param  array    $parameters    Parameters
  * @param  string   $httpMethod    HTTP method to use
  * @param  array    $options       Request options
  *
  * @return array    HTTP response
  */
 protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
 {
     $response = $this->browser->call($url, $httpMethod, $this->headers, json_encode($parameters));
     $this->checkApiLimit($response);
     return array('response' => $response->getContent(), 'headers' => $response->getHeaders(), 'errorNumber' => '', 'errorMessage' => '');
 }
Beispiel #18
0
 /**
  * Отправляет HTTP request
  *
  * @param string $url     The url to fetch
  * @param string $content The content of the request
  * @param string $method  The HTTP method to use
  *
  * @return Response
  */
 protected function httpRequest($url, $content = null, $method = Request::METHOD_GET)
 {
     $browser = new Browser(new Curl());
     $response = $browser->call($url, $method, array(), $content);
     return $response;
 }
Beispiel #19
0
 /**
  * @param string $url
  * @param mixed  $method
  *
  * @return string
  */
 protected function httpRequest($url, $method = Request::METHOD_GET)
 {
     $curl = new Curl();
     $curl->setVerifyPeer(false);
     $buzz = new Browser($curl);
     $response = $buzz->call($url, $method);
     return $response->getContent();
 }
Beispiel #20
0
 /**
  * @param string $url
  * @param mixed  $method
  *
  * @return string
  */
 protected function httpRequest($url, $method = RequestInterface::METHOD_GET)
 {
     $buzz = new Browser(new Curl());
     try {
         $response = $buzz->call($url, $method);
     } catch (\RuntimeException $e) {
         return false;
     }
     return $response->getContent();
 }