Skip to content

PSR 7: Uri Example

Terry L edited this page Jun 20, 2020 · 3 revisions
Clone this wiki locally

PSR-7 HTTP Message Interfaces

Namespace

Shieldon\Psr7\Uri

Uri

__construct

  • param string uri = "" The URI.

Example:

$uri = new \Shieldon\Psr7\Uri('https://www.example.com');

getScheme()

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://www.example.com'
);
echo $uri->getScheme();
// Outputs: https

getAuthority()

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getAuthority();
// Outputs: terry:1234@example.com:8888

getUserInfo()

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getUserInfo();
// Outputs: terry:1234

getHost()

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getHost();
// Outputs: example.com

getPort()

  • return int|null

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getPort();
// Outputs: 8888

getPath

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://example.com/post/?p=113&foo=bar#yes-i-do'
);
echo $uri->getPath();
// Outputs: /post/

getQuery

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://example.com/post/?p=113&foo=bar#yes-i-do'
);
echo $uri->getQuery();
// Outputs: p=113&foo=bar

getFragment

  • return string

Example:

$uri = new \Shieldon\Psr7\Uri(
    'https://example.com/post/?p=113&foo=bar#yes-i-do'
);
echo $uri->getFragment();
// Outputs: yes-i-do

withScheme($scheme)

  • param string scheme * The scheme to use with the new instance.
  • return static

Example:

echo $uri->getScheme();
// Outputs: https

$url = $uri->withScheme('http');
echo $uri->getScheme();
// Outputs: http

withUserInfo($user, $password)

  • param string user * The user name to use for authority.

  • param string|null password = null The password associated with $user.

  • return static

Example:

echo $uri->getUserInfo();
// Outputs: terry:1234

$url = $uri->withUserInfo('jack', '5678');
echo $uri->getUserInfo();
// Outputs: jack:5678

withHost($host)

  • param string host * The hostname to use with the new instance.
  • return static

Example:

echo $uri->getHost();
// Outputs: example.com

$url = $uri->withHost('terryl.in');
echo $uri->getHost();
// Outputs: terryl.in

withPort($port)

  • param int|null port * The port to use with the new instance; a null value removes the port information.
  • return static

Example:

echo $uri->getPort();
// Outputs: 8888

$uri = $uri->withPort(443);
echo $uri->getPort();
// Outputs: 443

$uri = $uri->withPort(null);
echo $uri->getPort();
// Outputs:

withPath($path)

  • param string path * The path to use with the new instance.
  • return static

Example:

echo $uri->getPath();
// Outputs: /post/

$uri = $uri->withPath('/new-path');
echo $uri->getPath();
// Outputs: /new-path

withQuery($query)

  • param string query * The query string to use with the new instance.
  • return static

Example:

echo $uri->getQuery();
// Outputs: p=113&foo=bar

$uri = $uri->witQuery('p=120&foo=baz');
echo $uri->getQuery();
// Outputs: p=120&foo=baz

withFragment($fragment)

  • param string fragment * The fragment to use with the new instance.
  • return static

Example:

echo $uri->getFragment();
// Outputs: yes-i-do

$uri = $uri->withFragment('no-i-cant');
echo $uri->getFragment();
// Outputs: no-i-cant

__toString

  • return string

Example:

$uri = new Uri('http://example.com:8888/demo/#section-1');
echo $uri;
// Outputs: http://example.com:8888/demo/#section-1