PHP code example of jakeasmith / http_build_url

1. Go to this page and download the library: Download jakeasmith/http_build_url library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jakeasmith / http_build_url example snippets


use Uri\Rfc3986\Uri;

// Replace parts of a URL
echo Uri::parse('https://example.com/search?q=php#top')
    ->withPath('/docs')
    ->withQuery('page=2')
    ->withFragment(null)
    ->toString();
// https://example.com/docs?page=2

// Join a relative path and merge query strings
// (was HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY)
$base = Uri::parse('https://example.com/v1/users?page=2');
parse_str($base->getQuery() ?? '', $query);
echo $base->resolve('me')
    ->withQuery(http_build_query($query + ['fields' => 'name']))
    ->toString();
// https://example.com/v1/me?page=2&fields=name