前回に引き続きGuzzle バージョン3という古い環境でのお話ですが。
Guzzle\Http\Urlというクラスが同梱されているので、Net_URL2
の代わりにでも使おうとして、相対URIの解決ではまったのでメモ。
相対URIを解決するcombine
メソッドが、デフォルトではおかしな解決をしてしまうのです。
(因みに最新のバージョン4では問題ないです)
uri.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
$baseUri= 'http://example.net/sample/path';
$relativeUri = 'relative/path';
echo 'Base URI: ', $baseUri, PHP_EOL;
echo 'URI to combine: ', $relativeUri, PHP_EOL;
$uri = \Guzzle\Http\Url::factory($baseUri);
echo 'Default : ', $uri->combine($relativeUri), PHP_EOL;
$uri = \Guzzle\Http\Url::factory($baseUri);
echo 'RFC 3986: ', $uri->combine($relativeUri, true), PHP_EOL;
$uri = \GuzzleHttp\Url::fromString($baseUri);
echo 'Guzzle 4: ', $uri->combine($relativeUri), PHP_EOL;
$ php ./uri.php
Base URI: http://example.net/sample/path
URI to combine: relative/path
Default : http://example.net/sample/path/relative/path
RFC 3986: http://example.net/sample/relative/path
Guzzle 4: http://example.net/sample/relative/path
↑の通り、combine
の第二引数にtrue
を渡すと、ちゃんと解決してくれます。