url

URL parser, builder, query string encoder/decoder, percent-encoding and path joiner.

v1.0.0Web & networkingbundledimport "url";
Overview Installation Documentation Examples

Overview

URL parser, builder, query string encoder/decoder, percent-encoding, and path joiner for HolyPHP with clear validation errors for malformed URLs.

bundled — works offline uri query http

Quick start

import "url";

$u = url_parse("https://user:pass@example.com:8443/api/v1?page=2&q=hello+world#top");
println($u->scheme);   // "https"
println($u->host);     // "example.com"
println($u->port);     // 8443
println($u->path);     // "/api/v1"

$params = $u->queryParams();
println($params["q"]); // "hello world"

$next = $u->joinPath("users/42")
          ->withQueryParams(["active" => "true", "role" => "admin"])
          ->withFragment("profile");
println(url_build($next));
// "https://user:pass@example.com:8443/api/v1/users/42?active=true&role=admin#profile"

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install url

This adds the library to hphp.yaml, installs it into .hphp/packages/url/ and pins the exact version in hphp.lock:

require:
  url: "^1.0"

Use it

import "url";

url is bundled with the compiler, so import "url"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install url          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install url
Packageurl
Version1.0.0 (follows the compiler)
Entry fileurl.hphp
Sourcepackages/lib/url ↗

Documentation

API Reference

Url Class

Represents a parsed or constructed URL.

Properties

Constructor

new Url(
    string $scheme = "",
    string $host = "",
    int $port = 0,
    string $path = "",
    string $query = "",
    string $fragment = "",
    string $userinfo = ""
)

Methods

Functions

Error Handling

All validation and parsing errors throw Exception with a descriptive url: ... message:

Examples

01Quick start

Bundled with the compiler — import "url"; works offline in any file, or add it to a project with hphp install url.

import "url";

$u = url_parse("https://user:pass@example.com:8443/api/v1?page=2&q=hello+world#top");
println($u->scheme);   // "https"
println($u->host);     // "example.com"
println($u->port);     // 8443
println($u->path);     // "/api/v1"

$params = $u->queryParams();
println($params["q"]); // "hello world"

$next = $u->joinPath("users/42")
          ->withQueryParams(["active" => "true", "role" => "admin"])
          ->withFragment("profile");
println(url_build($next));
// "https://user:pass@example.com:8443/api/v1/users/42?active=true&role=admin#profile"
Try it
hphp install url
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_url.hphp.