2010-01-06 61 views
2

捲曲下載http://mysite.com/Lunacy%20Disc%202%20of%202%20(U)(Saturn).zip爲什麼不能curl以不同的格式下載相同的URL?

但不

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

爲什麼會出現這種情況?

我是否需要將其轉換爲第一種格式?

使用通過urlencode($ url)生成的URL失敗。

回答

2

兩個問題:

  1. urlencode也將編碼你的斜線。這意味着要將查詢字符串編碼爲用於網址,而不是完整的網址。
  2. urlencode將空格編碼爲+。如果您想將空格設置爲%20,則需要rawurlencode
0

你需要urlencode翻譯空間(在你的例子中,還有其他需要它的字符)在互聯網上傳輸。編碼可以確保各種通信協議在處理它時不會終止或破壞字符串。

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. 

在這裏,你知道爲什麼空格字符來轉義/編碼太;-)

+0

curl以urlencoded字符串失敗 – 2010-01-06 20:42:46

0

http://mysite.com/Lunacy 2(U)盤2(土星).Z ip

這不是一個有效的網址。訪問這樣的網址可能會在您的瀏覽器中正常工作,因爲如果需要,大多數現代瀏覽器都會自動爲您編碼網址。捲曲庫不能自動執行此操作。

1

urlencode()確實失敗,捲曲,如果你的問題是隻是空格,您可以手動替換它們

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

爲什麼?由於某些字符具有特殊含義,如#(html錨)。

所以所有的字符除了字母數字不管編碼與否都編碼。

相關問題