A PHP library providing common URL implementation.
{
"require": {
"chroma-x/url-util": "~2.0"
}
}
require_once('path/to/vendor/autoload.php');
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"
Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.
PHP URL Util is under the MIT license.