Dek*_*kel 2 php symfony goutte guzzle domcrawler
我有一个 Goutte/Client(goutte 使用 symfony 来处理请求),我想加入路径并获得最终 URL:
$client = new Goutte\Client();
$crawler = $client->request('GET', 'http://DOMAIN/some/path/')
// $crawler is instance of Symfony\Component\DomCrawler\Crawler
$new_path = '../new_page';
$final path = $crawler->someMagicFunction($new_path);
// final path == http://DOMAIN/some/new_page
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种简单的方法,将$new_path变量与请求中的当前页面连接起来并获取新的 URL。
请注意,$new_page可以是以下任意一个:
new_page ==> http://DOMAIN/some/path/new_page
../new_page ==> http://DOMAIN/some/new_page
/new_page ==> http://DOMAIN/new_page
Run Code Online (Sandbox Code Playgroud)
symfony/goutte/guzzle 是否提供了任何简单的方法来做到这一点?
我找到了getUriForPathfrom Symfony\Component\HttpFoundation\Request,但我没有看到任何简单的方法将 the 转换Symfony\Component\BrowserKit\Request为HttpFoundation\Request
Uri::resolve()从包装中使用guzzlehttp/prs7。此方法允许您从基本部分和相关部分创建规范化URL。
一个例子(使用优秀的psysh shell):
\n\nPsy Shell v0.7.2 (PHP 7.0.12 \xe2\x80\x94 cli) by Justin Hileman\n>>> $base = new GuzzleHttp\\Psr7\\Uri(\'http://example.com/some/dir\')\n=> GuzzleHttp\\Psr7\\Uri {#208}\n>>> (string) GuzzleHttp\\Psr7\\Uri::resolve($base, \'/new_base/next/next/../../back_2\')\n=> "http://example.com/new_base/back_2"\nRun Code Online (Sandbox Code Playgroud)\n\n另请查看UriNormalizer 类。有一个与您的问题相关的示例(测试用例)。
\n\n从测试用例来看:
\n\n$uri = new Uri(\'http://example.org/../a/b/../c/./d.html\');\n$normalizedUri = UriNormalizer::normalize($uri, UriNormalizer::REMOVE_DOT_SEGMENTS);\n\n$this->assertSame(\'http://example.org/a/c/d.html\', (string) $normalizedUri);\nRun Code Online (Sandbox Code Playgroud)\n