public function forwardAction() { $alias = $this->params('alias'); $instance = $this->getInstanceManager()->getInstanceFromRequest(); try { $location = $this->aliasManager->findCanonicalAlias($alias, $instance); $this->redirect()->toUrl($location); $this->getResponse()->setStatusCode(301); return false; } catch (CanonicalUrlNotFoundException $e) { } try { $source = $this->aliasManager->findSourceByAlias($alias); } catch (AliasNotFoundException $e) { $this->getResponse()->setStatusCode(404); return false; } $router = $this->getServiceLocator()->get('Router'); $request = new Request(); $request->setMethod(Request::METHOD_GET); $request->setUri($source); $routeMatch = $router->match($request); if ($routeMatch === null) { $this->getResponse()->setStatusCode(404); return false; } $this->getEvent()->setRouteMatch($routeMatch); $params = $routeMatch->getParams(); $controller = $params['controller']; $return = $this->forward()->dispatch($controller, ArrayUtils::merge($params, ['forwarded' => true])); return $return; }
public function findRegion($country, $query) { $request = new Request(); $request->setMethod(Request::METHOD_GET); foreach ($query as $key => $value) { $request->getQuery()->set($key, $value); } $request->getHeaders()->addHeaderLine('Accept', 'application/json'); switch ($country) { case 'CH': $request->setUri($this->config['url'] . '/ch-region'); break; default: $request->setUri($this->config['url'] . '/ch-region'); break; } $client = new Client(); $response = $client->send($request); $body = $response->getBody(); $result = json_decode($body, true); if ($result) { return $result['_embedded']['ch_region']; } /*echo "<textarea cols='100' rows='30' style='position:relative; z-index:10000; width:inherit; height:200px;'>"; print_r($body); echo "</textarea>"; die();*/ return null; }
/** * @covers JmlHelpers\View\Helper\QueryParams */ public function testQuery() { $helper = new QueryParams(); $helper->setServiceLocator($this->pluginManager); $this->request->setUri('http://example.com'); $values = $helper->__invoke(); $this->assertEquals($values, array()); $this->request->setUri('http://example.com?foo=bar&snafu=baz'); $values = $helper->__invoke(); $this->assertEquals($values, array('foo' => 'bar', 'snafu' => 'baz')); $this->request->setUri('http://example.com?foo=&snafu=baz'); $values = $helper->__invoke(); $this->assertEquals($values, array('foo' => '', 'snafu' => 'baz')); }
/** * @param $ipString * @return IdentityInformation * @throws \Zend\Validator\Exception\InvalidArgumentException */ public function getIpInfo($ipString) { $ipValidator = new Ip(); if (!$ipValidator->isValid($ipString)) { throw new InvalidArgumentException(); } // creating request object $request = new Request(); $request->setUri($this->endPoint . $ipString . '/json'); $client = new Client(); $adapter = new Client\Adapter\Curl(); $adapter->setCurlOption(CURLOPT_TIMEOUT_MS, 500); $client->setAdapter($adapter); $response = $client->send($request); $data = $response->getBody(); $dataArray = json_decode($data); $identityInformation = new IdentityInformation(); $identityInformation->setCountry(isset($dataArray->country) ? $dataArray->country : ''); $identityInformation->setRegion(isset($dataArray->region) ? $dataArray->region : ''); $identityInformation->setCity(isset($dataArray->city) ? $dataArray->city : ''); $identityInformation->setLocation(isset($dataArray->loc) ? $dataArray->loc : ''); $identityInformation->setProvider(isset($dataArray->org) ? $dataArray->org : ''); $identityInformation->setHostName(isset($dataArray->hostname) ? $dataArray->hostname : ''); return $identityInformation; }
/** * @inheritdoc */ public function request(array $params) { try { $request = new Request(); $headers = $request->getHeaders(); $request->setUri($params['url']); $headers->addHeaderLine('Accept-Encoding', 'gzip,deflate'); if ($params['config']->isAuthenticationPossible() === true && $this->option['keys']['public'] !== null && $this->option['keys']['private'] !== null) { /** * Note: DATE_RFC1123 my not be RFC 1123 compliant, depending on your platform. * @link http://www.php.net/manual/de/function.gmdate.php#25031 */ $date = gmdate('D, d M Y H:i:s \\G\\M\\T'); $path = $request->getUri()->getPath(); $headers->addHeaderLine('Date', $date); $headers->addHeaderLine('Authorization', $this->signRequest('GET', $date, $path)); } if (isset($params['lastmodified'])) { $headers->addHeaderLine('If-Modified-Since', $params['lastmodified']); } $response = $this->client->send($request); $body = $response->getBody(); $headers = null; if ($this->option['responseheader']) { $headers = $response->getHeaders()->toArray(); $this->lastResponseHeaders = $headers; } } catch (\Exception $e) { throw new ClientException('Client exception catched, use getPrevious().', 0, $e); } return $this->createResponse($params['config']->isJson(), $response->getStatusCode(), $body, $headers); }
public function matchUri($uri) { $request = new Request(); $request->setUri($uri); $request->setMethod('post'); return $this->getRouter()->match($request); }
/** * @dataProvider routeProvider * @param Query $route * @param string $path * @param integer $offset * @param array $params */ public function testMatching(Query $route, $path, $offset, array $params = null) { $request = new Request(); $request->setUri('http://example.com?' . $path); $match = $route->match($request, $offset); $this->assertInstanceOf('Zend\Mvc\Router\RouteMatch', $match); }
public function pharAction() { $client = $this->serviceLocator->get('zendServerClient'); $client = new Client(); if (defined('PHAR')) { // the file from which the application was started is the phar file to replace $file = $_SERVER['SCRIPT_FILENAME']; } else { $file = dirname($_SERVER['SCRIPT_FILENAME']) . '/zs-client.phar'; } $request = new Request(); $request->setMethod(Request::METHOD_GET); $request->setHeaders(Headers::fromString('If-Modified-Since: ' . gmdate('D, d M Y H:i:s T', filemtime($file)))); $request->setUri('https://github.com/zendtech/ZendServerSDK/raw/master/bin/zs-client.phar'); //$client->getAdapter()->setOptions(array('sslcapath' => __DIR__.'/../../../certs/')); $client->setAdapter(new Curl()); $response = $client->send($request); if ($response->getStatusCode() == 304) { return 'Already up-to-date.'; } else { ErrorHandler::start(); rename($file, $file . '.' . date('YmdHi') . '.backup'); $handler = fopen($file, 'w'); fwrite($handler, $response->getBody()); fclose($handler); ErrorHandler::stop(true); return 'The phar file was updated successfully.'; } }
/** * Prepare a Zend Request by Operation with $parameters * * @param Operation $operation * @param array $parameters * @param int $options BitMask of options to skip or something else * @return Request * @throws \InvalidArgumentException * @throws \RuntimeException */ public function makeRequestByOperation(Operation $operation, array $parameters = [], $options = 0) { $request = new Request(); $path = $operation->path; if ($operation->parameters) { foreach ($operation->parameters as $parameter) { if (isset($parameters[$parameter->name])) { switch ($parameter->in) { case 'path': $path = str_replace('{' . $parameter->name . '}', $parameters[$parameter->name], $path); break; case 'query': $request->getQuery()->set($parameter->name, $parameters[$parameter->name]); break; case 'formData': $request->getPost()->set($parameter->name, $parameters[$parameter->name]); break; default: throw new RuntimeException(sprintf('Parameter "%s" with ->in = "%s" is not supported', $parameter->parameter, $parameter->in)); } } elseif ($parameter->required && !($options & SwaggerWrapper::SKIP_REQUIRED)) { throw new InvalidArgumentException(sprintf('Parameter "%s" is required, please pass value for this in $parameters', $parameter->name)); } } } $request->setUri($path); $request->setMethod($operation->method); return $request; }
/** * {@inheritdoc} */ public function sendRequest(RequestInterface $request) { $request = $this->sanitizeRequest($request); $headers = new Headers(); foreach ($request->getHeaders() as $key => $value) { $headers->addHeader(new GenericHeader($key, $request->getHeaderLine($key))); } $zendRequest = new Request(); $zendRequest->setMethod($request->getMethod()); $zendRequest->setUri((string) $request->getUri()); $zendRequest->setHeaders($headers); $zendRequest->setContent($request->getBody()->getContents()); $options = ['httpversion' => $request->getProtocolVersion()]; if (extension_loaded('curl')) { $options['curloptions'] = [CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request->getProtocolVersion())]; } $this->client->setOptions($options); if ($this->client->getAdapter() instanceof ZendClient\Adapter\Curl && $request->getMethod()) { $request = $request->withHeader('Content-Length', '0'); } try { $zendResponse = $this->client->send($zendRequest); } catch (RuntimeException $exception) { throw new NetworkException($exception->getMessage(), $request, $exception); } return $this->responseFactory->createResponse($zendResponse->getStatusCode(), $zendResponse->getReasonPhrase(), $zendResponse->getHeaders()->toArray(), $zendResponse->getContent(), $zendResponse->getVersion()); }
public function callServer($method, $params) { // Get the URI and Url Elements $apiUrl = $this->generateUrl($method); $requestUri = $apiUrl['uri']; // Convert the params to something MC can understand $params = $this->processParams($params); $params["apikey"] = $this->getConfig('apiKey'); $request = new Request(); $request->setMethod(Request::METHOD_POST); $request->setUri($requestUri); $request->getHeaders()->addHeaders(array('Host' => $apiUrl['host'], 'User-Agent' => 'MCAPI/' . $this->getConfig('apiVersion'), 'Content-type' => 'application/x-www-form-urlencoded')); $client = new Client(); $client->setRequest($request); $client->setParameterPost($params); $result = $client->send(); if ($result->getHeaders()->get('X-MailChimp-API-Error-Code')) { $error = unserialize($result->getBody()); if (isset($error['error'])) { throw new MailchimpException('The mailchimp API has returned an error (' . $error['code'] . ': ' . $error['error'] . ')'); return false; } else { throw new MailchimpException('There was an unspecified error'); return false; } } return $result->getBody(); }
/** * Send request elasticsearch like this : * curl -X{$httpMethod} http://host/type/index/{$elasticMethod} -d '{json_decode($content)}' * @param int $httpMethod * @param string $elasticMethod * @param string $content * * @return Stdlib\ResponseInterface */ public function sendRequest($httpMethod = Request::METHOD_GET, $elasticMethod = null, $content = null) { $request = new Request(); $request->setUri($this->url . $elasticMethod)->setMethod($httpMethod)->setContent($content); $client = new Client(); return $client->dispatch($request); }
function PlanJSONManager($action, $url, $requestjson, $uid) { $request = new Request(); $request->getHeaders()->addHeaders(array('Content-Type' => 'application/json; charset=UTF-8')); //$url=""; try { $request->setUri($url); $request->setMethod($action); $client = new Client(); if ($action == 'PUT' || $action == 'POST') { $client->setUri($url); $client->setMethod($action); $client->setRawBody($requestjson); $client->setEncType('application/json'); $response = $client->send(); return $response; } else { $response = $client->dispatch($request); //var_dump(json_decode($response->getBody(),true)); return $response; } } catch (\Exception $e) { $e->getTrace(); } return null; }
public function testToUriStringMultiQueryOverwrite() { $request = new Request(); $request->setUri('http://google.ca/test.html?foo=bar'); $request->getQuery()->set('foo', 'value'); $this->assertEquals('http://google.ca/test.html?foo=value', RequestUtils::toUriString($request)); }
public function testNoMatchingOnDifferentScheme() { $request = new Request(); $request->setUri('http://example.com/'); $route = new Scheme('https'); $match = $route->match($request); $this->assertNull($match); }
/** * @param string $method * @param string $url * * @return null|string */ public function match($method, $url) { $request = new Request(); $request->setMethod($method); $request->setUri($url); $routeMatch = $this->router->match($request); return $routeMatch instanceof RouteMatch ? $routeMatch->getMatchedRouteName() : null; }
/** * @dataProvider routeProvider * @param HttpMethod $route * @param $verb * @internal param string $path * @internal param int $offset * @internal param bool $shouldMatch */ public function testMatching(HttpMethod $route, $verb) { $request = new Request(); $request->setUri('http://example.com'); $request->setMethod($verb); $match = $route->match($request); $this->assertInstanceOf('Zend\\Mvc\\Router\\Http\\RouteMatch', $match); }
public function testMatching() { $request = new Request(); $request->setUri('https://example.com/'); $route = new Scheme('https'); $match = $route->match($request); $this->assertInstanceOf('Zend\\Mvc\\Router\\Http\\RouteMatch', $match); }
/** * @covers JmlHelpers\View\Helper\CurrentRoute */ public function testCurrentRoute() { $helper = new CurrentRoute(); $helper->setServiceLocator($this->pluginManager); $this->request->setUri('http://example.com'); $url = $helper->__invoke(); $this->assertEquals($url, 'home'); $this->request->setUri('http://example.com/foo/bar'); $url = $helper->__invoke(); $this->assertEquals($url, 'default'); $this->request->setUri('http://example.com/foo'); $url = $helper->__invoke(); $this->assertEquals($url, 'default'); $this->request->setUri('http://example.com/foo/bar/baz'); $url = $helper->__invoke(); $this->assertEquals($url, ''); }
/** * @param Request $request * @return Response */ protected function proceed(Http $httpUri, Request $request) { $httpUri = $httpUri->parse($this->moduleOptions->getApiUrl() . $httpUri->toString()); $request->setUri($httpUri); $this->httpClient->setAuth($this->moduleOptions->getUserName(), $this->moduleOptions->getPassword()); $this->httpClient->setOptions(array('sslverifypeer' => false)); return $this->httpClient->send($request); }
public function getByUrl($url, $params) { $router = $this->sm->get('router'); $uri = new Http($url); $request = new Request(); $request->setUri($uri); $match = $router->match($request); return $this->getByRoute($match->getMatchedRouteName(), $params); }
public function testRequestHttpGet() { $client = new HTTPClient(); $request = new Request(); $request->setUri($this->baseuri); $request->setMethod(Request::METHOD_GET); $response = $client->send($request); $this->assertTrue($response->isSuccess()); }
public function testIsActiveReturnsFalseOnNonMatchingRequestUri() { $page = new Page\Uri(array('label' => 'foo', 'uri' => '/bar')); $request = new Request(); $request->setUri('/baz'); $request->setMethod('GET'); $page->setRequest($request); $this->assertFalse($page->isActive()); }
private function preparePostRequest($post) { $postRequest = new Request(); $postRequest->setMethod(Request::METHOD_POST); $postRequest->setPost($post); $postRequest->setUri($this->url); $postRequest->getHeaders()->addHeaders(['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8']); return $postRequest; }
private function prevController($uri) { $request = new Request(); $request->setUri($uri); $router = $this->serviceLocator->get('Router'); $routeMatch = $router->match($request); if ($routeMatch) { return $routeMatch->getParam('controller'); } return ''; }
/** * @param array $regexpes * @param string $uriPath * @param bool $expectedResult * @dataProvider shouldCacheProvider */ public function testShouldCache($regexpes, $uriPath, $expectedResult) { $this->strategy->setRegexpes($regexpes); $mvcEvent = new MvcEvent(); $request = new HttpRequest(); $uri = new Http(); $uri->setPath($uriPath); $request->setUri($uri); $mvcEvent->setRequest($request); $this->assertEquals($expectedResult, $this->strategy->shouldCache($mvcEvent)); }
/** * Builds a request object for the given API path * * @param string $path * @return Zend\Http\Request */ protected function buildRequest($path) { $request = new Request(); $request->setUri('https://api2.dynect.net/REST' . $path); $headers = $request->getHeaders(); $headers->addHeaderLine('Content-Type', 'application/json'); $headers->addHeaderLine('API-Version', '3.5.8'); if ($this->token) { $headers->addHeaderLine('Auth-Token', $this->token); } return $request; }
/** * @dataProvider routeProvider * @param Literal $route * @param string $path * @param integer $offset * @param boolean $shouldMatch */ public function testMatching(Literal $route, $path, $offset, $shouldMatch) { $request = new Request(); $request->setUri('http://example.com' . $path); $match = $route->match($request, $offset); if (!$shouldMatch) { $this->assertNull($match); } else { $this->assertInstanceOf('Zend\\Mvc\\Router\\Http\\RouteMatch', $match); if ($offset === null) { $this->assertEquals(strlen($path), $match->getLength()); } } }
private function matchUrl($url, $urlHelper) { $url = 'http://localhost.localdomain' . $url; $request = new Request(); $request->setUri($url); $router = new TreeRouteStack(); $router->addRoute('hostname', ['type' => 'hostname', 'options' => ['route' => 'localhost.localdomain'], 'child_routes' => ['resource' => ['type' => 'segment', 'options' => ['route' => '/resource[/:id]']]]]); $match = $router->match($request); if ($match instanceof RouteMatch) { $urlHelper->setRouter($router); $urlHelper->setRouteMatch($match); } return $match; }
/** * @dataProvider routeProvider * @param Hostname $route * @param string $hostname * @param array $params */ public function testMatching(Hostname $route, $hostname, array $params = null) { $request = new Request(); $request->setUri('http://' . $hostname . '/'); $match = $route->match($request); if ($params === null) { $this->assertNull($match); } else { $this->assertInstanceOf('Zend\\Mvc\\Router\\Http\\RouteMatch', $match); foreach ($params as $key => $value) { $this->assertEquals($value, $match->getParam($key)); } } }