2

私は 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 はそうする簡単な方法を提供しますか?

getUriForPathfromを見つけましたがSymfony\Component\HttpFoundation\Request、 を に変換する簡単な方法がわかりませんSymfony\Component\BrowserKit\RequestHttpFoundation\Request

4

2 に答える 2

4

パッケージUri::resolve()から使用します。このメソッドを使用すると、ベース部分と相対部分から正規化guzzlehttp/prs7された 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も見てください。あなたの問題に関連する例(テストケース)があります。

テストケースから:

$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);
于 2016-11-28T09:58:55.887 に答える
1

parse_urlURLのパスを取得するために使用できます:

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

次に、それを正規化する方法が必要です。この回答は次のことに役立ちます。

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);
}

あとは URL を再構築するだけです。次のコメントを参照してください。

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))));
于 2016-11-27T15:48:19.773 に答える