Skip to content

Instantly share code, notes, and snippets.

@tylerreed
Created December 11, 2012 18:54
Show Gist options
  • Save tylerreed/4261015 to your computer and use it in GitHub Desktop.
Save tylerreed/4261015 to your computer and use it in GitHub Desktop.
Build a Request URL from an Array
<?php
/* Example of $url and $params */
$url = 'http://api.host.com/';
$params['key1'] = 'value1';
$params['key2'] = 'value2';
/* --------------------------- */
function buildRequestURL( $url, $params ) {
$request_url = $url . '?';
$i = 0;
foreach ( $params as $param => $value ) {
if( $i > 0 ) {
$request_url .= '&';
}
$request_url .= $param . '=' . urlencode( $value );
$i++;
}
return $request_url;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment