How to use http_build_url with pecl_http version 2.0 with PHP 5.4, 5.5, and 5.6

The old way to use http_build_url

This is how to use http_build_url with pecl_http version 1.x:

$parse = parse_url('http://example.com/index.asp?test=123#hash');

print_r($parse);
#
# Yields a result of:
#
# Array
# (
#     [scheme] => http
#     [host] => example.com
#     [path] => /index.asp
#     [query] => test=123
#     [fragment] => hash
# )

$url = http_build_url($parse);
echo $url;
#
# Yields a result of:
#
# http://example.com/index.asp?test=123#hash

The new way to use http_build_url with pecl_http version 2.x:

This functionality has now moved into the http\Url class:

$parse = parse_url('http://example.com/index.asp?test=123#hash');

print_r($parse);
#
# Yields a result of:
#
# Array
# (
#     [scheme] => http
#     [host] => example.com
#     [path] => /index.asp
#     [query] => test=123
#     [fragment] => hash
# )

$url = new \http\Url($parse);
echo $url->toString();
#
# Yields a result of:
#
# http://example.com/index.asp?test=123#hash
#

Related Posts:

  • No Related Posts
This entry was posted in Tech Tips and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *