Skip to content

A PHP library providing common URL implementation.

License

Notifications You must be signed in to change notification settings

chroma-x/php-url-util

Repository files navigation

PHP URL Util

Build Status Test Coverage Dependency Status SensioLabs Insight Code Climate Latest Stable Version Total Downloads License

A PHP library providing common URL implementation.

Installation

{
   	"require": {
        "chroma-x/url-util": "~2.0"
    }
}

Usage

Autoloading and namesapce

require_once('path/to/vendor/autoload.php');

Parsing an URL

use ChromaX\UrlUtil;

$url = new UrlUtil\Url('https://john:secret@mydomain.com:8443/path/to/resource?arg1=123&arg2=test#fragment');

$scheme = $url->getScheme();
fwrite(STDOUT, 'Scheme "' . $scheme . '"' . PHP_EOL);

$hostname = $url->getHostname();
fwrite(STDOUT, 'Hostname "' . $hostname . '"' . PHP_EOL);

$port = $url->getPort();
fwrite(STDOUT, 'Port "' . (string)$port . '"' . PHP_EOL);

$username = $url->getUsername();
fwrite(STDOUT, 'Username "' . $username . '"' . PHP_EOL);

$password = $url->getPassword();
fwrite(STDOUT, 'Password "' . $password . '"' . PHP_EOL);

$path = $url->getPath();
fwrite(STDOUT, 'Path "' . $path . '"' . PHP_EOL);

$queryParameters = $url->getQueryParameters();
foreach ($queryParameters as $queryParameter) {
	fwrite(STDOUT, 'Query parameter "' . $queryParameter->getKey() . '" is "' . $queryParameter->getValue() . '"' . PHP_EOL);
}

$fragment = $url->getFragment();
fwrite(STDOUT, 'Fragment "' . $fragment . '"' . PHP_EOL);

$url
	->setScheme('http')
	->setHostname('yourdomain.com')
	->setPort(8080)
	->setUsername('doe')
	->setPassword('supersecret')
	->setPath('path/to/another/resource')
	->removeQueryParameterByKey('arg2')
	->addQueryParameter(new UrlUtil\QueryParameter('arg1', '456'))
	->addQueryParameter(new UrlUtil\QueryParameter('arg3', 'test'))
	->setFragment('target');

fwrite(STDOUT, 'URL "' . $url->buildUrl() . '"' . PHP_EOL);

will output the following

Scheme "https"
Hostname "mydomain.com"
Port "8443"
Username "john"
Password "secret"
Path "/path/to/resource"
Query parameter "arg1" is "123"
Query parameter "arg2" is "test"
Fragment "fragment"
URL "http://doe:supersecret@yourdomain.com:8080/path/to/another/resource?arg1=456&arg3=test#target"

Contribution

Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.

License

PHP URL Util is under the MIT license.