bart
123
1
General

Generate query URL

Posted 1 year ago by bart

Hey everybody,

I'm searching for a method in PHP that generates a full query URL by passing a base URL (which could already include a query string) plus additional parameters.

I know that I could check if the URL contains a "?" and if so append with "&", otherwise append with "?". The question here is, if PHP has such a method on board?

I'll give you an example:

// Only works if $baseUrl not includes parameters
public function buildQuery($baseUrl, array $params) {
    return $baseUrl . '?' . http_build_query($params);
}

// But what if I do this?
$url = $this->buildQuery('https://my.endpoint.com/fetch?subject=php', ['maxlength' => 10]);

Any ideas?

Thanks a lot!!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.