/** * Add parameters for the request * @param \Network\Http\Request $Request request instance * @param array $get GET method parameters * @param array $post POST method parameters */ private function addRequestParameters(Request &$Request, array $get = null, array $post = null) { if (!is_null($get)) { foreach ($get as $key => $value) { $Request->addGetField($key, $value); } } if (!is_null($post)) { foreach ($post as $key => $value) { $Request->addPostField($key, $value); } } }
<?php /** * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The F**k You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://www.wtfpl.net/ for more details. * * Request example with url parametrization * @author alxmsl * @date 2/6/13 */ // Firstly include base class include '../source/Autoloader.php'; use alxmsl\Network\Http\Request; // Create request object $Request = new Request(); $Request->setUrl('https://www.googleapis.com/androidpublisher/v1/')->setConnectTimeout(3)->setTimeout(5); // Setup transport $Request->setTransport(Request::TRANSPORT_CURL); // Set url data fields $Request->addUrlField('applications', 'com.my.application')->addUrlField('subscriptions', 'com.my.application.subscription.1')->addUrlField('token', 'some access token'); // Send request $data = $Request->send(); var_dump($data);
/** * Getter for the request * @param string $url request url * @return Request request object */ protected function getRequest($url) { $Request = new Request(); $Request->setTransport(Request::TRANSPORT_CURL); return $Request->setUrl($url)->setConnectTimeout($this->getConnectTimeout())->setTimeout($this->getRequestTimeout()); }
<?php /** * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The F**k You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://www.wtfpl.net/ for more details. * * POST request example * @author alxmsl * @date 1/25/13 */ // Firstly include base class include '../source/Autoloader.php'; use alxmsl\Network\Http\Request; // Create request object $Request = new Request(); $Request->setUrl('http://api.topface.com')->setConnectTimeout(3)->setTimeout(5); // Setup transport $Request->setTransport(Request::TRANSPORT_CURL); // Set POST data fields $Request->addPostField('field1', 'value1'); $Request->addPostField('field2', 'value2'); // Send request $data = $Request->send(); var_dump($data);
/** * Getter for the request * @param string $url request url * @return Request request object */ protected function getRequest($url) { $Request = new Request(); $Request->setTransport(Request::TRANSPORT_CURL); return $Request->setUrl($url)->setConnectTimeout($this->getConnectTimeout())->setTimeout($this->getRequestTimeout())->setContentTypeCode(Request::CONTENT_TYPE_JSON); }
/** * Create request for method call * @param string $methodName API method name * @param array $parameters method call parameters * @return Request request object */ private function getRequest($methodName, array $parameters = []) { $Request = new Request(); $Request->setTransport(Request::TRANSPORT_CURL); $Request->setUrl(self::ENDPOINT_URI)->setConnectTimeout($this->getConnectTimeout())->setTimeout($this->getRequestTimeout())->addUrlField(sprintf('bot%s', $this->token), $methodName); if (!empty($parameters)) { $Request->setPostData($parameters); } return $Request; }
/** * Send command for Purchases Subscriptions API * @param Request $Request request instance * @return string API response body * @throws InvalidCredentialsException when access grants is not granted * @throws ErrorException when API error acquired * @throws TransportException when HTTP transport error occurred * @throws UnexpectedValueException when access token is empty for client */ private function makeRequest(Request $Request) { $accessToken = $this->getAccessToken(); if (!empty($accessToken)) { try { return $Request->send(); } catch (HttpClientErrorCodeException $Ex) { switch ($Ex->getCode()) { case 401: throw InvalidCredentialsException::initializeByString($Ex->getMessage()); default: throw ErrorException::initializeByString($Ex->getMessage()); } } catch (HttpServerErrorCodeException $Ex) { throw ErrorException::initializeByString($Ex->getMessage()); } } else { throw new UnexpectedValueException('access token is empty'); } }
/** * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The F**k You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://www.wtfpl.net/ for more details. * * Request signification example * @author alxmsl * @date 3/30/13 */ // Firstly include base class include '../source/Autoloader.php'; use alxmsl\Network\Http\Request; // Create request object $Request = new Request(); $Request->setUrl('http://topface.com'); // Setup transport $Request->setTransport(Request::TRANSPORT_CURL); // Add request data $Request->addGetField('a', 'value_a'); $Request->addGetField('b', 'value_b'); $Request->addPostField('c', 'value_c'); $Request->addPostField('d', 'value_d'); // Create custom request data signature $sign = $Request->getSignature(function (Request $Request) { $sign = ''; $get = $Request->getGetData(); if (!empty($get)) { $sign = http_build_query($get); }
/** * Append parameters to HTTP request * @param HttpRequest $HttpRequest HTTP request instance */ private function appendParameters(HttpRequest $HttpRequest) { switch ($HttpRequest->getMethod()) { case HttpRequest::METHOD_GET: foreach ($this->parameters as $field => $value) { $HttpRequest->addGetField($field, $value); } break; case HttpRequest::METHOD_POST: $HttpRequest->setPostData($this->parameters); break; } }
<?php /** * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The F**k You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://www.wtfpl.net/ for more details. * * GET request example * @author alxmsl * @date 1/13/13 */ // Firstly include base class include '../source/Autoloader.php'; use alxmsl\Network\Http\Request; // Create request object $Request = new Request(); $Request->setUrl('http://topface.com')->setConnectTimeout(3)->setTimeout(5); // Setup transport $Request->setTransport(Request::TRANSPORT_CURL); // Send request $data = $Request->send(); var_dump($data);
/** * Create request method * @param Message $Message message instance * @return Request HTTP request instance */ private function createRequest(Message $Message) { $Request = new Request(); $Request->setTransport(Request::TRANSPORT_CURL); $Request->setUrl(self::ENDPOINT_URI)->setContentTypeCode(Request::CONTENT_TYPE_TEXT)->setConnectTimeout($this->getConnectTimeout())->setTimeout($this->getRequestTimeout()); $data = array_merge(['user='******'from=' . $this->from, 'sign=' . $this->sign($Message)], $Message->export()); $Request->setPostData(implode('&', $data)); return $Request; }