Php 为什么可以';是否下载不同格式的相同URL?

Php 为什么可以';是否下载不同格式的相同URL?,php,curl,Php,Curl,curl下载http://mysite.com/Lunacy%20Disc%202%20of%202%20(U) (土星)。邮编 但不是 http://mysite.com/Lunacy Disc 2 of 2 (U)(Saturn).zip 为什么会这样 我需要将其转换为第一种格式吗 使用通过urlencode($URL)生成的URL失败。您需要使用urlencode来翻译空格(在您的示例中,还有其他字符需要它),以便在internet上传输。编码确保各种通信协议在处理字符串时不会终止或以其

curl下载
http://mysite.com/Lunacy%20Disc%202%20of%202%20(U) (土星)。邮编

但不是

http://mysite.com/Lunacy Disc 2 of 2 (U)(Saturn).zip
为什么会这样

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


使用通过urlencode($URL)生成的URL失败。

您需要使用urlencode来翻译空格(在您的示例中,还有其他字符需要它),以便在internet上传输。编码确保各种通信协议在处理字符串时不会终止或以其他方式损坏字符串

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


现在,关于“为什么”,答案可能可以在

引用一些段落:

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/2(U)(土星).zip

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

urlencode()
确实无法使用curl,如果您的问题只是空格,您可以手动替换它们

$url = str_replace(' ', '%20', $url);
两个问题:

  • urlencode
    还将对您身上的斜杠进行编码。这意味着对查询字符串进行编码,以便在URL中使用,而不是完整的URL
  • urlencode
    将空格编码为
    +
    。如果希望将空格设置为
    %20
    ,则需要

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

    因此,除alfanumeric字符外,所有字符都被编码,无论是否需要编码