Can you use urlunsplit (or urlunparse?) like you did above, instead of building this URL yourself?