PHP Conference Japan 2024

curl_escape

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

curl_escapeURL 編碼指定的字串

說明

curl_escape(CurlHandle $handle, string $string): string|false

此函式會根據 » RFC 3986 對指定的字串進行 URL 編碼。

參數

handle

curl_init() 返回的 cURL 句柄。

string

要編碼的字串。

返回值

傳回已跳脫的字串,如果失敗則傳回 false

更新日誌

版本 說明
8.0.0 handle 現在需要一個 CurlHandle 實例;以前需要的是 資源

範例

範例 #1 curl_escape() 範例

<?php
// 建立一個 curl 句柄
$ch = curl_init();

// 跳脫用作 GET 參數的字串
$location = curl_escape($ch, 'Hofbräuhaus / München');
// 結果:Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen

// 使用跳脫後的字串組成 URL
$url = "http://example.com/add_location.php?location={$location}";
// 結果:http://example.com/add_location.php?location=Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen

// 發送 HTTP 請求並關閉句柄
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>

參見

新增註釋

使用者貢獻的註釋 3 則註釋

Franois
10 年前
此函式與 rawurlencode() 完全相同。

它在內部使用 libcurl 的 curl_easy_escape(),其文件說明:「此函式將給定的輸入字串轉換為 URL 編碼字串 (… )。所有不是 a-z、A-Z、0-9、'-'、'.'、'_' 或 '~' 的輸入字元都會轉換為其「URL 跳脫」版本 (%NN,其中 NN 是兩位數的十六進位數字)。
sam dot tyurenkov at gmail dot com
4 年前
請提供一個在參數之間跳脫「&」符號的例子。

例如,使用 curl 處理以下網址的正確方法是什麼?
https://example.com/?p1=1&p2=2&p3=3

這點並不直觀,需要說明。
Nico
10 年前
這個函式和 urlencode() 有什麼不同?
To Top