How do I avoid URL globbing with PHP cURL?

2019-05-13 06:29发布

问题:

I have a url (slightly modified) like so:

https://ssl.site.com/certificate/123/moo.shoo?type=456&domain=$GH$%2fdodo%20[10%3a47%3a11%3a3316]

It doesn't work the way I intend it to when passed straight through to PHP cURL because of the brackets.

I managed to run the same URL successfully in the command line like so:

curl -g "https://ssl.site.com/certificate/123/moo.shoo?type=456&domain=$GH$%2fdodo%20[10%3a47%3a11%3a3316]"

Is there an option (similar to -g, for disabling globbing) that I can use in PHP cURL? If not, how should I encode or format my URL before passing it to PHP cURL?

回答1:

Currently I'm using this and it seems to work

$urlReconstructed = str_replace(']', '%5D', str_replace('[', '%5B', $url));


回答2:

This seems to work for me:

$urlParts = parse_url($url);    
parse_str($urlParts['query'], $queryParts);
$urlReconstructed = sprintf('%s://%s%s?', $urlParts['scheme'], $urlParts['host'], $urlParts['path']);

foreach ($queryParts as $key => $value)
{
  $urlReconstructed .= $key . "=" . urlencode($value);
}

echo $urlReconstructed;

Thanks Pekka, Convert your comment to an answer. If no other better answers pop up i will award you the correct answer.