Created
August 11, 2023 07:36
-
-
Save nyamsprod/045f3fa4d0361aa1e93b7a526d5935c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\Idna\Converter as IdnConverter; | |
use League\Uri\IPv4\Converter as Ipv4Converter; | |
echo IdnConverter::toAscii('bébé.be')->domain(), PHP_EOL; //display "xn--bb-bjab.be" | |
echo Ipv4Converter::fromEnvironment()->toDecimal('192.87.125'); //display "192.87.0.125" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\BaseUri; | |
use League\Uri\Modifier; | |
$uri = "https://www.bbc.co.uk"; | |
$resolveUri = BaseUri::from($uri) | |
->resolve("path/to/../../the/sky/?foo.bar"); | |
$newUri = Modifier::from($resolveUri) | |
->appendQuery('foo.bar=tata') | |
->removeLabels(1) | |
->removeTrailingSlash() | |
->getUriString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\Uri; | |
use League\Uri\UriModifier; | |
use League\Uri\UriResolver; | |
$uri = "https://www.bbc.co.uk"; | |
$relativeUri = "path/to/../../the/sky/?foo.bar"; | |
$baseUri = Uri::createFromString($uri); | |
$relativeUri = Uri::createFromString($relativeUri); | |
$resolveUri = UriResolver::resolve($relativeUri, $baseUri); | |
$newUri = UriModifier::appendQuery($resolveUri, 'foo.bar=tata'); | |
$newUri = UriModifier::removeLabels($newUri, 1); | |
$newUri = UriModifier::removeTrailingSlash($newUri); | |
$newUri = $newUri->__toString(); | |
echo $newUri, PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\UriTemplate; | |
$template = 'https://api.example.com/{version}/search/{term}/{?q*,limit}'; | |
$params = [ | |
'term' => ['john', 'doe'], | |
'q' => ['a', 'b'], | |
'limit' => '10', | |
]; | |
$uriTemplate = new UriTemplate($template); | |
echo $uriTemplate->expand($params); | |
// display https://api.example.com//search/john,doe/?q=a&q=b&limit=10 with missing version | |
// this is valid and follow the specifications | |
echo $uriTemplate->expandOrFail($params); | |
// will throw a TemplateCanNotBeExpanded exception with the following message | |
// Missing variables `version` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment