resolve() public static method

Converts the relative URI into a new URI that is resolved against the base URI.
See also: UriResolver::resolve
Deprecation: since version 1.4. Use UriResolver::resolve instead.
public static resolve ( Psr\Http\Message\UriInterface $base, string | Psr\Http\Message\UriInterface $rel ) : Psr\Http\Message\UriInterface
$base Psr\Http\Message\UriInterface Base URI
$rel string | Psr\Http\Message\UriInterface Relative URI
return Psr\Http\Message\UriInterface
示例#1
0
 public function getForm($formId, $params, $headers)
 {
     /** @var DOMElement $form */
     $dom = new DomDocument();
     libxml_use_internal_errors(true);
     $dom->loadHTML($this->response());
     $xpath = new DOMXpath($dom);
     $form = $xpath->query("//form[@id='{$formId}']")->item(0);
     $elements = $xpath->query('//input');
     $form_params = [];
     $allowedTypes = ["hidden", "text", "password"];
     foreach ($elements as $element) {
         /** @var DOMElement $element */
         $type = $element->getAttribute("type");
         if (in_array($type, $allowedTypes)) {
             $name = $element->getAttribute("name");
             $value = $element->getAttribute("value");
             $form_params[$name] = $value;
         }
     }
     $headers = array_merge(["Referer" => $this->baseUri], $headers);
     $url = Uri::resolve(new Uri($this->baseUri), $form->getAttribute("action"))->__toString();
     $method = strtoupper($form->getAttribute("method"));
     return ["method" => $method, "url" => $url, "headers" => $headers, "params" => array_merge($form_params, $params)];
 }
示例#2
0
 /**
  * Convert relative links, images scr and form actions to absolute
  *
  * @param ElementFinder $page
  * @param string $affectedUrl
  */
 public static function convertUrlsToAbsolute(ElementFinder $page, $affectedUrl)
 {
     $affected = new Uri($affectedUrl);
     $srcElements = $page->element('//*[@src] | //*[@href] | //form[@action]');
     $baseUrl = $page->value('//base/@href')->getFirst();
     foreach ($srcElements as $element) {
         $attributeName = 'href';
         if ($element->hasAttribute('action') === true and $element->tagName === 'form') {
             $attributeName = 'action';
         } else {
             if ($element->hasAttribute('src') === true) {
                 $attributeName = 'src';
             }
         }
         $relative = $element->getAttribute($attributeName);
         # don`t change javascript in href
         if (preg_match('!^\\s*javascript\\s*:\\s*!', $relative)) {
             continue;
         }
         if (parse_url($relative) === false) {
             continue;
         }
         if (!empty($baseUrl) and !preg_match('!^(/|http)!i', $relative)) {
             $relative = Uri::resolve(new Uri($baseUrl), $relative);
         }
         $url = Uri::resolve($affected, (string) $relative);
         $element->setAttribute($attributeName, (string) $url);
     }
 }
 /**
  * @param string $filename
  * @param string $version
  *
  * @return \Psr\Http\Message\UriInterface
  */
 public function uriFor($filename, $version = '')
 {
     $filename = original_version($filename);
     if ($version) {
         $filename .= ":{$version}";
     }
     return Uri::resolve($this->baseUri, $filename);
 }
示例#4
0
文件: Apist.php 项目: noxify/apist
 /**
  * @param $httpMethod
  * @param $url
  * @param $blueprint
  * @param array $options
  *
  * @return array
  * @throws \GuzzleHttp\Exception\GuzzleException
  */
 protected function request($httpMethod, $url, $blueprint, $options = [])
 {
     $url = Uri::resolve($this->getBaseUrl(), $url);
     $this->currentMethod = new ApistMethod($this->getGuzzle(), $url, $blueprint);
     $this->lastMethod = $this->currentMethod;
     $this->currentMethod->setMethod($httpMethod);
     $result = $this->currentMethod->get($options);
     $this->currentMethod = null;
     return $result;
 }
示例#5
0
 public function get($url, array $options = [], \Closure $processor = null)
 {
     $url = Psr7\Uri::resolve($this->baseUri, $url);
     $response = $this->httpClient->get($url, $options);
     if (null !== $processor) {
         $response = $processor($response);
     }
     $crawler = new Crawler(null, (string) $url, (string) $this->baseUri);
     $crawler->addContent($response->getBody());
     return $crawler;
 }
示例#6
0
 /**
  * @param  string       $attribute
  * @param  UriInterface $base
  */
 private function resolveLinkAttribute($attribute, UriInterface $base)
 {
     $elements = $this->xpath->query("//*[@{$attribute} and not(contains(@{$attribute}, \"://\"))]");
     foreach ($elements as $element) {
         try {
             $resolved = Uri::resolve($base, $element->getAttribute($attribute));
             $element->setAttribute($attribute, $resolved->__toString());
         } catch (InvalidArgumentException $e) {
             // Tolerate invalid urls
         }
     }
 }
示例#7
0
 /**
  * Moves the URI of the queue to the URI in the input parameter.
  *
  * @return callable
  */
 private function queueUrl()
 {
     return static function (callable $handler) {
         return function (CommandInterface $c, RequestInterface $r = null) use($handler) {
             if ($c->hasParam('QueueUrl')) {
                 $uri = Uri::resolve($r->getUri(), $c['QueueUrl']);
                 $r = $r->withUri($uri);
             }
             return $handler($c, $r);
         };
     };
 }
 /**
  * @param RequestInterface $request
  * @param bool             $forceNew
  *
  * @return RequestInterface
  */
 public function signRequest(RequestInterface $request, $forceNew = false)
 {
     try {
         // force-get a new token if it was requested, if not, the regular
         // caching mechanism will be used so the call is not necessary here.
         if (true === $forceNew) {
             $this->pool->getToken($forceNew);
         }
         // create a new request with the new uri and the token added to the headers
         $uri = Uri::resolve(new Uri($this->pool->getEndpointUrl()), $request->getUri());
         return $request->withUri($uri)->withHeader('X-Auth-Token', $this->pool->getTokenId());
     } catch (TokenException $e) {
         throw new ClientException('Could not obtain token', $request, null, $e);
     }
 }
 /**
  * @param $endpoint
  * @param ServerRequestInterface $request
  *
  * @return ResponseInterface
  */
 public function proxy($endpoint, ServerRequestInterface $request)
 {
     /** @var ClientInterface $client */
     $client = $this->container->get('bangpound_guzzle_proxy.client.' . $endpoint);
     $rel = $request->getAttribute('path');
     if ($request->getQueryParams()) {
         $rel .= '?' . \GuzzleHttp\Psr7\build_query($request->getQueryParams());
     }
     $rel = new Uri($rel);
     $uri = $client->getConfig('base_url');
     $uri = new Uri($uri);
     $uri = Uri::resolve($uri, $rel);
     $request = \GuzzleHttp\Psr7\modify_request($request, array('uri' => $uri));
     $response = $client->send($request);
     if ($response->hasHeader('Transfer-Encoding')) {
         $response = $response->withoutHeader('Transfer-Encoding');
     }
     return $response;
 }
示例#10
0
 public static function get($path, $query = [])
 {
     if (!isset(self::$guzzle)) {
         self::initUsingConfigFile();
     }
     if (is_array($query)) {
         $query = self::weedOut($query);
         $query = http_build_query($query, null, '&', PHP_QUERY_RFC3986);
         $query = preg_replace('/%5[bB]\\d+%5[dD]=/', '=', $query);
     }
     $response = self::$guzzle->get($path, ['query' => $query]);
     switch ($response->getStatusCode()) {
         case 200:
             return json_decode($response->getBody(), true);
         case 404:
             return null;
         default:
             $uri = Psr7\Uri::resolve(Psr7\uri_for(self::$guzzle->getConfig('base_uri')), $path);
             $uri = $uri->withQuery($query);
             throw new ServerException($response->getStatusCode() . " " . $response->getReasonPhrase() . ". URI: " . $uri);
     }
 }
示例#11
0
 /**
  * @dataProvider getResolveTestCases
  */
 public function testResolvesUris($base, $rel, $expected)
 {
     $uri = new Uri($base);
     $actual = Uri::resolve($uri, $rel);
     $this->assertEquals($expected, (string) $actual);
 }
示例#12
0
 private function buildUri($uri, array $config)
 {
     // for BC we accept null which would otherwise fail in uri_for
     $uri = Psr7\uri_for($uri === null ? '' : $uri);
     if (isset($config['base_uri'])) {
         $uri = Psr7\Uri::resolve(Psr7\uri_for($config['base_uri']), $uri);
     }
     return $uri->getScheme() === '' ? $uri->withScheme('http') : $uri;
 }
示例#13
0
 private function buildUri($uri, array $config)
 {
     if (!isset($config['base_uri'])) {
         return $uri instanceof UriInterface ? $uri : new Psr7\Uri($uri);
     }
     return Psr7\Uri::resolve(Psr7\uri_for($config['base_uri']), $uri);
 }
示例#14
0
 private function fixUri(Step $step, $uri)
 {
     if (!$step->getEndpoint()) {
         return $uri;
     }
     return Psr7\Uri::resolve(Psr7\uri_for($step->getEndpoint()), $uri);
 }
 private function createRequest($uri)
 {
     $baseUri = \GuzzleHttp\Psr7\uri_for($this->client->getConfig('base_uri'));
     $uri = Uri::resolve($baseUri, $uri);
     return new Request('GET', $uri);
 }
示例#16
0
 public function createRequest($method, $uri, array $options = [])
 {
     /** @var \Psr\Http\Message\RequestInterface $request */
     $request = clone $this->defaultRequest;
     $request = $request->withMethod($method);
     $request = $request->withUri(GuzzlePsr7\Uri::resolve($request->getUri(), $uri));
     $request = $this->applyOptions($request, $options);
     return $request;
 }
 private function buildEndpoint(Operation $operation, array $args, array $opts)
 {
     $varspecs = [];
     // Create an associative array of varspecs used in expansions
     foreach ($operation->getInput()->getMembers() as $name => $member) {
         if ($member['location'] == 'uri') {
             $varspecs[$member['locationName'] ?: $name] = isset($args[$name]) ? $args[$name] : null;
         }
     }
     $relative = preg_replace_callback('/\\{([^\\}]+)\\}/', function (array $matches) use($varspecs) {
         $isGreedy = substr($matches[1], -1, 1) == '+';
         $k = $isGreedy ? substr($matches[1], 0, -1) : $matches[1];
         if (!isset($varspecs[$k])) {
             return '';
         } elseif ($isGreedy) {
             return str_replace('%2F', '/', rawurlencode($varspecs[$k]));
         } else {
             return rawurlencode($varspecs[$k]);
         }
     }, $operation['http']['requestUri']);
     // Add the query string variables or appending to one if needed.
     if (!empty($opts['query'])) {
         $append = Psr7\build_query($opts['query']);
         $relative .= strpos($relative, '?') ? "&{$append}" : "?{$append}";
     }
     // Expand path place holders using Amazon's slightly different URI
     // template syntax.
     return Psr7\Uri::resolve($this->endpoint, $relative);
 }
示例#18
0
 /**
  * @inheritdoc
  */
 public function getObjectUrl(SwiftObject $object)
 {
     return Uri::resolve($this->getBaseUri(), $object->getPath());
 }
示例#19
0
 /**
  * @param int $id
  * @param int|null $pageLimit
  * @return array
  * @throws NotFoundException
  * @throws ScrapeException
  */
 public function getNewspaper($id, $pageLimit = null)
 {
     $response = $this->getClient()->get("newspaper/{$id}")->send();
     if (!$response->isRedirect()) {
         throw new ScrapeException();
     }
     $location = $response->getLocation();
     if ($location == '/en') {
         throw new NotFoundException("Newspaper ID:{$id} does not exist.");
     }
     $xs = $this->getClient()->get($location)->send()->xpath();
     $paginator = new Paginator($xs);
     $info = $xs->find('//div[@class="newspaper_head"]');
     $avatar = $info->find('//img[@class="avatar"]/@src')->extract();
     $url = explode('/', $info->find('div[@class="info"]/h1/a[1]/@href')->extract())[3];
     $director = $info->find('div[2]/ul[1]/li[1]/a[1]');
     $desc = $xs->find('//meta[@name="description"]/@content')->extract();
     if (!preg_match('/has (\\d+) articles/', $desc, $articlesCount)) {
         throw new ScrapeException();
     }
     $em = EntityManager::getInstance();
     $countries = $em->getRepository(Country::class);
     $result = ['director' => ['id' => (int) explode('/', $director->find('@href')->extract())[4], 'name' => $director->find('@title')->extract()], 'name' => $info->find('//h1/a/@title')->extract(), 'url' => Uri::resolve($this->getClient()->getBaseUri(), $location), 'avatar' => str_replace('55x55', '100x100', $avatar), 'country' => $countries->findOneByName($info->find('div[1]/a[1]/img[2]/@title')->extract()), 'subscribers' => (int) $info->find('div[@class="actions"]')->extract(), 'article_count' => (int) $articlesCount[1], 'articles' => []];
     $pages = $paginator->getLastPage();
     if ($pageLimit !== null && $pages > $pageLimit) {
         $pages = $pageLimit;
     }
     for ($page = 1; $page <= $pages; $page++) {
         $xs = $this->getClient()->get('newspaper/' . $url . '/' . $page)->send()->xpath();
         foreach ($xs->findAll('//div[@class="post"]') as $art) {
             $title = $art->find('div[2]/h2/a')->extract();
             $artUrl = 'http://www.erepublik.com' . $art->find('div[2]/h2/a/@href')->extract();
             $votes = $art->find('div[1]/div[1]/strong')->extract();
             $comments = $art->find('div[2]/div[1]/a[1]')->extract();
             $date = $art->find('div[2]/div[1]/em')->extract();
             try {
                 $category = trim($art->find('div[2]/div[1]/a[3]')->extract());
             } catch (NodeNotFoundException $e) {
                 $category = null;
             }
             $result['articles'][] = ['title' => $title, 'url' => $artUrl, 'votes' => (int) $votes, 'comments' => (int) $comments, 'date' => self::parseDate($date), 'category' => $category];
         }
     }
     return $result;
 }
示例#20
0
 /**
  * Set the appropriate URL on the request based on the location header
  *
  * @param RequestInterface  $request
  * @param ResponseInterface $response
  * @param array             $protocols
  *
  * @return UriInterface
  */
 private function redirectUri(RequestInterface $request, ResponseInterface $response, array $protocols)
 {
     $location = Psr7\Uri::resolve($request->getUri(), $response->getHeaderLine('Location'));
     // Ensure that the redirect URI is allowed based on the protocols.
     if (!in_array($location->getScheme(), $protocols)) {
         throw new BadResponseException(sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, implode(', ', $protocols)), $request, $response);
     }
     return $location;
 }
示例#21
0
 /**
  * @return string
  */
 protected function getAbsoluteUri()
 {
     $base = $this->client->getBaseUri();
     return !empty($this->uri) ? Uri::resolve(new Uri($base . '/'), $this->uri) : $base;
 }
 /**
  * @param string $method
  * @param UriInterface $uri
  * @return RequestInterface
  */
 public function create($method, UriInterface $uri)
 {
     $uri = Uri::resolve($this->baseUri, $uri);
     $headers = [self::API_KEY_HEADER => $this->apiKey->toNative()];
     return new Request($method, $uri, $headers);
 }
示例#23
0
 public function getAbsoluteUri($uri)
 {
     /** @var $baseUri Psr7Uri  **/
     $baseUri = $this->client->getConfig('base_uri');
     if (strpos($uri, '://') === false) {
         return new Psr7Uri(Uri::appendPath((string) $baseUri, $uri));
     }
     return Psr7Uri::resolve($baseUri, $uri);
 }
示例#24
0
 /**
  * @param string $path
  *
  * @return \Psr\Http\Message\UriInterface
  */
 public function buildUri($path)
 {
     return Psr7\Uri::resolve(Psr7\uri_for($this->baseUri), $path);
 }
 /**
  * @param string|Uri $uri
  * @return Uri
  */
 private function createUri($uri)
 {
     $baseUri = \GuzzleHttp\Psr7\uri_for($this->client->getConfig('base_uri'));
     return Uri::resolve($baseUri, $uri);
 }