1. 对 url 参数进行排序
public function sortUri($uri)
{
$parse = [
'scheme'=>'',
'host'=>'',
'user'=>'',
'pass'=>'',
'path'=>'',
'query'=>'',
'fragment'=>''
];
$parse = array_merge($parse, parse_url($uri));
parse_str($parse['query'], $query);
ksort($query);
$parse['query'] = http_build_query($query);
return $this->build_url($parse);
}
2. 逆向 parse_url 生成 url
public function build_url($parse)
{
$res = '';
if (!empty($parse['scheme']))
$res .= $parse['scheme'] . '://';
if (!empty($parse['user']))
$res .= $parse['user'];
if (!empty($parse['pass']))
$res .= ':' . $parse['pass'];
if (!empty($parse['user']))
$res .= '@';
if (!empty($parse['host']))
$res .= $parse['host'];
if (!empty($parse['path']))
$res .= $parse['path'];
if (!empty($parse['query']))
$res .= '?' . $parse['query'];
if (!empty($parse['fragment']))
$res .= '#' . $parse['fragment'];
return $res;
}
网友评论