为什么不能以不同的格式卷曲下载相同的URL?

时间:2010-01-06 20:07:06

标签: php curl

curl downloads http://mysite.com/Lunacy%20Disc%202%20of%202%20(U)(Saturn).zip

但不是

http://mysite.com/Lunacy Disc 2 of 2 (U)(Saturn).zip

为什么会这样?

我需要将其转换为第一种格式吗?

使用通过urlencode($ url)生成的网址失败。

6 个答案:

答案 0 :(得分:2)

两个问题:

  1. urlencode也会对你的斜杠进行编码。它的意思是编码查询字符串,以便在网址中使用,而不是完整的网址。
  2. urlencode将空格编码为+。如果您想要空格为%20,则需要rawurlencode

答案 1 :(得分:1)

要将URL转换为“第一格式”,您可以使用PHP函数urlencode


现在,对于“为什么”,可以在RFC 1738 - Uniform Resource Locators (URL)中找到答案。

引用一些段落:

Octets must be encoded if they have no corresponding graphic
character within the US-ASCII coded character set, if the use of the
corresponding character is unsafe, or if the corresponding character
is reserved for some other interpretation within the particular URL
scheme.

No corresponding graphic US-ASCII:

URLs are written only with the graphic printable characters of the
US-ASCII coded character set. The octets 80-FF hexadecimal are not
used in US-ASCII, and the octets 00-1F and 7F hexadecimal represent
control characters; these must be encoded.

一个空格的代码为%20 - 它不在00-1F的范围内,因此应该对其进行编码......但是,稍后:

Unsafe:

   Characters can be unsafe for a number of reasons.  The space
   character is unsafe because significant spaces may disappear and
   insignificant spaces may be introduced when URLs are transcribed or
   typeset or subjected to the treatment of word-processing programs.

在这里,你知道为什么空格字符也必须被转义/编码; - )

答案 2 :(得分:1)

urlencode()确实因curl而失败,如果你的问题只是空格,你可以手动替换它们

$url = str_replace(' ', '%20', $url);

答案 3 :(得分:0)

您需要urlencode来翻译空间(在您的示例中;还有其他需要它的字符)才能通过互联网进行传输。编码确保各种通信协议在处理时不会终止或以其他方式破坏字符串。

答案 4 :(得分:0)

  

http://mysite.com/Lunacy Disc 2 of 2(U)(Saturn).zip

这不是有效的网址。访问这样的网址可能会在您的浏览器中生效,因为大多数现代浏览器会根据需要自动为您编码网址。 curl库不能自动执行此操作。

答案 5 :(得分:0)

为什么呢?因为某些字符具有特殊含义,例如#(html anchor)。

所有字符除了alfanumeric 之外的所有字符都是编码的,无论是否需要编码。