Troubleshooting of curl errors in Mandarin

From LemonWiki共筆
Jump to navigation Jump to search

cUrl 是在命令行或腳本中用來傳輸資料的工具。(來源:cUrl 官網)(替代函式庫:(1) PHP 原生的 Client URL Library (2) PHP 的 cURL 類別 (3) 在 Windows 上透過 Cygwin 安裝 cUrl

🌐 Switch language: EN, 漢字


Owl icon.jpg 如何除錯 cUrl 命令:(1) 啟用 詳細模式 例如:「... 使用 -v / --verbose 選項執行命令以獲取更多資訊。」或 (2) 啟用 追蹤選項 例如:--trace-ascii [檔案名] 注意:如果同時啟用詳細模式和追蹤選項,詳細模式將覆蓋追蹤選項。

如何解決命令執行問題:在 DOS/終端機中可以運作,但在 PHP 中不運作

手動在 DOS 或 Windows 終端機(如 ConEmu) 中執行命令時可以運作,但透過 PHP 執行時可能不會產生預期的結果。常見的問題是命令沒有生成預期的檔案。

Windows 終端機的範例 cURL 命令

curl "https://www.example.com/path/to/resource/" ^
-H "authority: www.example.com" ^
-H "accept: text/html" ^
--data-raw "PARAMS" --compressed > output.html

解決方案:為 PHP 修訂的 cURL 命令

curl "https://www.example.com/path/to/resource/" -H "authority: www.example.com" -H "accept: text/html" --data-raw "PARAMS" > output.html

方案解釋

  • Single Continuous String in PHP: In PHP, when executing a shell command, avoid using the caret (^) for multi-line commands[1], which is typical in the Command Prompt. Instead, ensure the command is one continuous string.
  • Unsupported curl Options: If you encounter an error like "curl: option --compressed: the installed libcurl version doesn't support this", it's a clear indication to remove or adjust the problematic option in the curl command.


如何解決 cUrl 錯誤 (#5): 不支援的代理語法 (Unsupported proxy syntax)

問題狀況:socks5 代理的使用者名稱包含 @ 符號,例如 [email protected]

curl_setopt($ch, CURLOPT_PROXY, 'socks5://username:password@localhost:12345');
// result is ok

curl_setopt($ch, CURLOPT_PROXY, 'socks5://[email protected]:password@localhost:12345');
// met cUrl error (#5): Unsupported proxy syntax

解決方案

curl_setopt($ch, CURLOPT_PROXY, 'socks5://bob%40email.com:password@localhost:12345');
// result is ok

可以使用 URL Encode and Decode - Onlineurlencode 函數,跳脫 (Escape) 特殊符號[2]

如何解決 cUrl 錯誤 (#56):OpenSSL SSL_read: No error.

錯誤條件:Windows 上的 curl 版本 7.67.0 遇到問題。[curl_version](https://www.php.net/manual/en/function.curl-version.php) 返回的結果:

version: Array
(
    [version_number] => 475904
    [age] => 5
    [features] => 2953117
    [ssl_version_number] => 0
    [version] => 7.67.0
    [host] => x86_64-pc-win32
    [ssl_version] => OpenSSL/1.1.1d
    [libz_version] => 1.2.11
)

解決方案: 使用其他版本的 curl command or curl on Cygwin or cURL class for PHP.

相關文章


如何解決錯誤:error code: 1010

識別問題

  • 當我試圖透過 cURL 命令下載網頁內容時,遇到了一個問題,輸出顯示為「error code: 10100」。為了探索原因,重新執行了cURL命令並啟動了詳細模式(使用 -v 旗標)。顯示的標頭資訊包括:
< report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v4?s=xxx"}],"group":"cf-nel","max_age":604800}
< nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
< server: cloudflare

建議解決方案

  • 建議改成使用 JavaScript 的爬蟲來代替 cURL 命令的方案 [3]

如何解決錯誤 405 Method Not Allowed

當我嘗試爬取網路資源但遇到錯誤訊息:"錯誤 405 方法不允許"

解決方案:

  • 確保使用正確的 HTTP 請求方法 e.g. GETPOST 來存取網路資源。

如何解決錯誤 415 Unsupported Media Type

當我嘗試 POST JSON(作為有效負載)但遇到錯誤訊息:"錯誤 415 不支援的媒體類型"

解決方案:

  • 設定 cUrl 的標頭 (header) Content-Type: application/json [4]例如 cUrl 命令的語法如下:
curl -X POST --header "Content-Type: application/json" --data '{"user" : "your_user_name", "token" : "your_token"}' "https://example.com/"

如何解決 cURL 返回二進位資料,而非 html 或 json

如何解決下載的檔案是空的

錯誤條件

  • 當我執行了 Curl 命令 curl -O https://path/to/plain_text.txt,但下載的檔案是空的。
  • 啟用 詳細模式 後,發現了 HTTP 狀態碼 HTTP/1.1 302 Moved Temporarily。我添加了位置(-L)參數來解決下載的檔案是空的問題
執行命令結果: curl -v -O https://path/to/plain_text.txt

< HTTP/1.1 302 Moved Temporarily
< Server: nginx
< Date: Tue, 25 Aug 2020 03:45:13 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=60
< Location: /path/to/plain_text.txt

解決方案

curl -L -O https://path/to/plain_text.txt

如何解決錯誤:no matches found

錯誤條件

  • 當我執行了 Curl 命令 curl http://website.test/http_status.php?case=500,但我遇到了錯誤訊息:"zsh: 未找到匹配:http://website.test/http_status.php?case=500"

解決方案

  • 雙引號包住 URL e.g. curl "http://website.test/http_status.php?case=500"

相關資料

如何解決錯誤:Remote file name has no length

錯誤訊息

curl -O "https://website.com/?p=123"
curl: Remote file name has no length
curl: (23) Failed writing received data to disk/application

解決方案

curl "https://website.com/?p=123" -o myfile.html 

解決方案解釋

  • 當你在下載檔案時遇到錯誤,可能是因為 URL 不包含檔案名稱,導致 `curl` 無法確定要保存檔案的名稱。解決方案是使用 `-o` 選項手動指定檔案名稱,例如,`curl -o myfile.html "https://website.com/?p=123"`。此外,確保你有足夠的磁碟空間並且在你有寫入權限的目錄中。


如何解決錯誤:zsh: parse error near `&'

錯誤狀況

% curl -XPOST http://localhost:3000/ -H "Content-Type: application/json"  -d'{"url": "https://someweb.com/Today's_Biggest_Trends"}'
dquote>

解決方案: 跳脫單引號,例如' 改成 '' (單引號重複兩次)

% curl -XPOST http://localhost:3000/ -H "Content-Type: application/json"  -d'{"url": "https://someweb.com/Today''s_Biggest_Trends"}'

Trivial Solution Icon_exclaim.gif NOT WORK: single quote ' became \'

% curl -XPOST http://localhost:3000/ -H "Content-Type: application/json"  -d'{"url": "https://someweb.com/Today\'s_Biggest_Trends"}'
dquote>

延伸閱讀

References