加入symfony / goutte中的网址

时间:2016-11-27 14:55:51

标签: php symfony guzzle goutte 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

我正在寻找的是一种简单的方法,将$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

symfony / goutte / guzzle是否提供了任何简单的方法?

我找到了来自Symfony\Component\HttpFoundation\Request的{​​{3}},但我没有看到任何简单的方法将Symfony\Component\BrowserKit\Request转换为HttpFoundation\Request

2 个答案:

答案 0 :(得分:4)

使用guzzlehttp/prs7包中的Uri::resolve()。此方法允许您从基础和相关部分创建规范化 URL。

一个例子(使用优秀的psysh shell):

Psy Shell v0.7.2 (PHP 7.0.12 — cli) by Justin Hileman
>>> $base = new GuzzleHttp\Psr7\Uri('http://example.com/some/dir')
=> GuzzleHttp\Psr7\Uri {#208}
>>> (string) GuzzleHttp\Psr7\Uri::resolve($base, '/new_base/next/next/../../back_2')
=> "http://example.com/new_base/back_2"

另请查看UriNormalizer class。有一个示例(test case)与您的问题相关联。

从测试用例:

$uri = new Uri('http://example.org/../a/b/../c/./d.html');
$normalizedUri = UriNormalizer::normalize($uri, UriNormalizer::REMOVE_DOT_SEGMENTS);

$this->assertSame('http://example.org/a/c/d.html', (string) $normalizedUri);

答案 1 :(得分:1)

您可以使用parse_url获取网址:

$components = parse_url('http://DOMAIN/some/path/');
$path = $components['path'];

然后你需要一种方法来封装它。 This answer可以帮助您:

function normalizePath($path, $separator = '\\/')
{
    // Remove any kind of funky unicode whitespace
    $normalized = preg_replace('#\p{C}+|^\./#u', '', $path);

    // Path remove self referring paths ("/./").
    $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', $normalized);

    // Regex for resolving relative paths
    $regex = '#\/*[^/\.]+/\.\.#Uu';

    while (preg_match($regex, $normalized)) {
        $normalized = preg_replace($regex, '', $normalized);
    }

    if (preg_match('#/\.{2}|\.{2}/#', $normalized)) {
        throw new LogicException('Path is outside of the defined root, path: [' . $path . '], resolved: [' . $normalized . ']');
    }

    return trim($normalized, $separator);
}

剩下要做的就是重建网址,你可以看到this comment

function unparse_url($parsed_url) { 
    $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; 
    $host     = isset($parsed_url['host']) ? $parsed_url['host'] : ''; 
    $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; 
    $user     = isset($parsed_url['user']) ? $parsed_url['user'] : ''; 
    $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : ''; 
    $pass     = ($user || $pass) ? "$pass@" : ''; 
    $path     = isset($parsed_url['path']) ? $parsed_url['path'] : ''; 
    $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; 
    $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; 
    return "$scheme$user$pass$host$port/$path$query$fragment"; 
}

最终路径:

$new_path = '../new_page';

if (strpos($new_path, '/') === 0) { // absolute path, replace it entirely
    $path = $new_path;
} else { // relative path, append it
    $path = $path . $new_path;
}

把它们放在一起:

// http://DOMAIN/some/new_page
echo unparse_url(array_replace($components, array('path' => normalizePath($path))));