Skip to content

Commit

Permalink
New API function: http_build_url()
Browse files Browse the repository at this point in the history
Builds an URL from its components. This is basically the reverse of
parse_url(), a minimalistic implementation of pecl_http extension's
http_build_url() function [1].

Note: the function is wrapped in an if function exists block, so that we
use the extension's function if it is enabled.

[1] http://php.net/http-build-url
  • Loading branch information
dregad committed Mar 14, 2015
1 parent c2005d8 commit 60d1f1d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/http_api.php
Expand Up @@ -187,3 +187,27 @@ function http_all_headers() {
http_custom_headers();
}
}

if( !function_exists( 'http_build_url' ) ) {
/**
* Builds an URL from its components
* This is basically the reverse of parse_url(), a minimalistic implementation
* of pecl_http extension's http_build_url() function.
* @param array $p_url The URL components (as results from parse_url())
* @return string
*/
function http_build_url( array $p_url ) {
return
( isset( $p_url['scheme'] ) ? $p_url['scheme'] . '://' : '' )
. ( isset( $p_url['user'] )
? $p_url['user'] . ( isset( $p_url['pass'] ) ? ':' . $p_url['pass'] : '' ) .'@'
: ''
)
. ( isset( $p_url['host'] ) ? $p_url['host'] : '' )
. ( isset( $p_url['port'] ) ? ':' . $p_url['port'] : '' )
. ( isset( $p_url['path'] ) ? $p_url['path'] : '' )
. ( isset( $p_url['query'] ) ? '?' . $p_url['query'] : '' )
. ( isset( $p_url['fragment'] ) ? '#' . $p_url['fragment'] : '' )
;
}
}

0 comments on commit 60d1f1d

Please sign in to comment.