How do I avoid URL globbing with PHP cURL?

╄→尐↘猪︶ㄣ 提交于 2020-01-13 18:22:28

问题


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.



来源:https://stackoverflow.com/questions/5137027/how-do-i-avoid-url-globbing-with-php-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!