public function asset($resource = null, $params = []) { # http://example.com/app/images/foo.jpg $template = new UriTemplate($this->getBaseUrl(false)); $resource = '/' . ltrim($resource, '/'); return $template->expand($resource, $params); }
/** * Find the operationId associated to a given path and method * * @todo Implement a less expensive finder * @param string $method An HTTP method * @param string $path A path (ex: /foo/1) * * @return string The operationId */ public function findOperationId($method, $path) { $uriTemplateManager = new UriTemplate(); foreach ($this->requestDefinitions as $requestDefinition) { if ($requestDefinition->getMethod() !== $method) { continue; } $params = $uriTemplateManager->extract($requestDefinition->getPathTemplate(), $path, true); if ($params !== null) { return $requestDefinition->getOperationId(); } } throw new \InvalidArgumentException('Unable to resolve the operationId for path ' . $path); }
/** * @param string $relation * @param array $parameters * @throws ServiceUnavailableHttpException * @return Navigator|mixed */ public function command($relation, array $parameters = [], $files = []) { $templatedUrl = $this->getRelation($relation); // expand template url + remove templated params from parameters $url = $this->renderUri($templatedUrl, $parameters); $templatedParameters = $this->uriTemplater->extract($templatedUrl, $url); $parameters = array_diff_key($parameters, $templatedParameters); try { $headers = []; if (!$files) { $headers = ['Content-Type' => 'application/json']; $parameters = json_encode($parameters); } $request = $this->guzzleClient->post($url, $headers, $parameters); $request->addPostFiles($files); $command = $this->guzzleClient->send($request); } catch (ClientErrorResponseException $e) { throw $e; } switch ($command->getContentType()) { case 'application/hal+json': $response = new Navigator($command->json()); break; case 'application/json': $response = $command->json(); break; default: $response = $command->getBody(true); break; } return $response; }
public function route($method, $path) { $parser = new UriTemplate(); $results = []; $routes = $this->table[strtoupper($method)]; foreach ($routes as $route) { $args = $parser->extract($route[0], $path, true); if (isset($args)) { // reflect this function as late as possible, since *most* routes shouldn't be called $ref = new \ReflectionFunction($route[1]); $results[] = \Closure::bind(function () use($ref, $args) { return $ref->invokeArgs($args); }, null); } } // always return a callable function inside an array return empty($results) ? [$this->emptyRoute] : $results; }
/** * @param string $uri * @param array $variables * @return string * @todo look at returning UriInterface */ public function expandUri($uri, array $variables) { $template = new UriTemplate(); return $template->expand($uri, $variables); }
<?php require 'vendor/autoload.php'; require 'parser.php'; use Rize\UriTemplate; use Goutte\Client; $uri = new UriTemplate(); $client = new Client(); $parser = new Parser(); $url = $uri->expand('http://www.idchips.com/fr/recherche?search_identification_number={id}', ['id' => '999999999999999']); $crawler = $client->request('GET', $url); $content = $crawler->filter('div#content > table'); $text = null; $html = null; if (!empty($content)) { try { if (!empty($node = $content->eq(0))) { $text = $node->text(); $html = $node->html(); } } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } } //echo $text; $html = $parser->removeAttributes($html); echo $html;
/** * Create a complete API Uri from the Base Uri, path and query parameters. * * Example: * Given a base uri that equal http://domain.tld * Given the following parameters /pets/{id}, ['id' => 1], ['foo' => 'bar'] * Then the Uri will equal to http://domain.tld/pets/1?foo=bar * * @param string $pathTemplate A template path * @param array $pathParameters Path parameters * @param array $queryParameters Query parameters * * @return UriInterface */ private function buildRequestUri($pathTemplate, array $pathParameters, array $queryParameters) { $path = $this->uriTemplate->expand($pathTemplate, $pathParameters); $query = http_build_query($queryParameters); return $this->baseUri->withPath($path)->withQuery($query); }