LoginSignup
3
3

More than 5 years have passed since last update.

Guzzle v3での相対URIの解決

Posted at

前回に引き続き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を渡すと、ちゃんと解決してくれます。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3