Skip to content

Latest commit

 

History

History
208 lines (132 loc) · 3.26 KB

File metadata and controls

208 lines (132 loc) · 3.26 KB

URI Utility

Parse, build and manipulate URLs.

use Myerscode\Utilities\Web\UriUtility;

$utility = new UriUtility('https://example.com/path?foo=bar#section');

host(): string

Get the host component.

$utility->host(); // 'example.com'

scheme(): string

Get the URL scheme.

$utility->scheme(); // 'https'

isHttps(): bool

Check if the URL uses HTTPS.

$utility->isHttps(); // true

port(): int

Get the port (defaults to 80 for HTTP, 443 for HTTPS).

$utility->port(); // 443

path(): string

Get the URL path.

$utility->path(); // '/path'

query(): string

Get the raw query string.

$utility->query(); // 'foo=bar'

fragment(): string

Get the URL fragment (the part after #).

$utility->fragment(); // 'section'

getQueryParameters(): array

Get query parameters as an associative array.

$utility->getQueryParameters(); // ['foo' => 'bar']

addQueryParameter(string|array $params): self

Append query parameters (does not override existing keys).

$utility->addQueryParameter(['baz' => 'qux']);
$utility->addQueryParameter('baz=qux');

mergeQuery(string|array $params): self

Merge query parameters (overrides existing keys).

$utility->mergeQuery(['foo' => 'updated']);

setQuery(string|array $params): self

Replace the entire query string.

$utility->setQuery(['new' => 'value']);

removeQueryParameter(string $key): self

Remove a specific query parameter by key.

$utility->removeQueryParameter('foo');

hasQueryParameter(string $key): bool

Check if a query parameter exists.

$utility->hasQueryParameter('foo'); // true

withScheme(string $scheme): self

Set or replace the URL scheme.

$utility->withScheme('http');

withHost(string $host): self

Set or replace the URL host.

$utility->withHost('other.com');

withPath(string $path): self

Set or replace the URL path.

$utility->withPath('/new/path');

withPort(?int $port): self

Set or replace the URL port. Pass null to remove.

$utility->withPort(8080);

withFragment(string $fragment): self

Set or replace the URL fragment.

$utility->withFragment('top');

userInfo(): ?string

Get the user info component of the URI, or null if not present.

$utility->userInfo(); // null

isValid(): bool

Check if the URL is valid without making a request.

$utility->isValid(); // true

toArray(): array

Get all parsed URL components as an associative array.

$utility->toArray();
// ['scheme' => 'https', 'host' => 'example.com', 'port' => 443, 'path' => '/path', 'query' => 'foo=bar', 'fragment' => 'section']

equals(UriUtility $other): bool

Compare two URIs for equality.

$a = new UriUtility('https://example.com');
$b = new UriUtility('https://example.com');
$a->equals($b); // true

check(ResponseFrom $method): Response

Check the URL response using curl, headers, or HTTP client.

use Myerscode\Utilities\Web\Data\ResponseFrom;

$response = $utility->check(ResponseFrom::CURL);
$response->code(); // 200

value(): string

Get the full URL string.

$utility->value(); // 'https://example.com/path?foo=bar#section'