函式名稱:curl_escape()
適用版本:PHP 4 >= 4.0.2, PHP 5, PHP 7
函式描述:curl_escape() 函式用於在 URL 字串中轉義特殊字元。它將 URL 字串中的非字母數字和特殊字元轉換為它們的十六進位制表示形式,以便在 URL 中安全使用。
用法:
string curl_escape( resource $ch, string $string )
引數說明:
- $ch:一個 cURL 資源,使用 curl_init() 建立。
- $string:需要轉義的字串。
返回值:轉義後的字串。
示例:
// 建立一個cURL資源
$ch = curl_init();
// 設定要訪問的URL
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/?q=' . curl_escape($ch, 'search term'));
// 執行cURL請求
curl_exec($ch);
// 關閉資源
curl_close($ch);
在上述示例中,我們首先使用curl_escape()
函式將'search term'
字串轉義為 URL 安全的形式,並將其與 URL 字串拼接在一起。然後,我們使用 cURL 傳送請求,將轉義後的 URL 傳遞給CURLOPT_URL
選項。最後,我們關閉了 cURL 資源。這樣做可以確保查詢引數在請求中被正確地轉義並安全使用。