php cURL 请求 URL 中包含空格的 URL .. 该怎么做

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5931853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:56:12  来源:igfitidea点击:

cURL requesting URL with whitespaces in URL.. What to do

phpparsingcurlcommunication

提问by Atticus

So I'm trying to curl this URL:

所以我试图卷曲这个 URL:

http://images.fastcompany.com/upload/Screen shot 2011-04-28 at 8.13.21 PM.png

http://images.fastcompany.com/upload/Screen shot 2011-04-28 at 8.13.21 PM.png

URL Encoded it reads as:

URL Encoded 它读作:

http%3A%2F%2Fimages.fastcompany.com%2Fupload%2FScreen+shot+2011-04-28+at+8.13.21+PM.png

http%3A%2F%2Fimages.fastcompany.com%2Fupload%2FScreen+shot+2011-04-28+at+8.13.21+PM.png

However, curl needs it to be decoded into a proper URL obviously.

但是,curl 显然需要将其解码为正确的 URL。

How do i get around this problem? cURL drops off the rest of the string as soon as it reaches any whitespace... :(

我如何解决这个问题?一旦 cURL 到达任何空格,cURL 就会从字符串的其余部分消失...... :(

I should mention I can't wrap the URL with double quotes as it is a variable being posted.

我应该提到我不能用双引号将 URL 括起来,因为它是一个正在发布的变量。

Edit: hahahahaha wowwwwww brainfart.. thanks guys :P

编辑:hahahahaha wowwwwww Brainfart ..谢谢大家:P

回答by Kane Wallmann

Just use str_replace.

只需使用 str_replace。

echo str_replace ( ' ', '%20', 'http://images.fastcompany.com/upload/Screen shot 2011-04-28 at 8.13.21 PM.png' );

回答by Drew

Perhaps try replacing spaces with %20?

也许尝试用%20?

回答by Hoàng V? Tgtt

I use:

我用:

$link = trim($link);
$link = str_replace ( ' ', '%20', $link);

回答by Juanma Menendez

For me just to put the name with spaces between ""worked.

对我来说,只是在“”之间加上空格就行了。

Example

例子

curl --upload-file "001- name - lastName.pdf" https://transfer.sh/ernesto

Notice the use of "" in "001- name - lastName.pdf"

注意“001-name-lastName.pdf”中“”的使用

回答by Mitchell M

Use the str_replace();function. Replace your " "with "%20"

使用该str_replace();功能。替换你" ""%20"