將 curl 命令轉換為 PHP 程式碼 - 為 API 請求生成即用型 PHP cURL 程式碼
"https://api.example.com/data", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode(["name" => "test"]), CURLOPT_HTTPHEADER => [ "Content-Type: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }
以下是一些可以轉換為 PHP 程式碼的常見 curl 命令:
curl https://api.example.com/users
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","email":"[email protected]"}' https://api.example.com/users
curl -X PUT -H "Authorization: Bearer token123" -d '{"status":"active"}' https://api.example.com/users/1
curl -X DELETE https://api.example.com/users/1
curl -H "X-API-Key: abc123" -H "Accept: application/json" https://api.example.com/data
PHP 的 cURL 擴展提供了一種強大的方式來發出 HTTP 請求。以下是一些常見的 PHP cURL 模式:
"https://api.example.com/upload", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => [ 'file' => $cfile ], CURLOPT_HTTPHEADER => [ "Authorization: Bearer YOUR_TOKEN_HERE" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }
"https://api.example.com/data", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 5, // 5 seconds timeout CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", ]); $response = curl_exec($curl); $err = curl_error($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($err) { echo "cURL Error: " . $err; } else { if ($httpCode >= 400) { echo "HTTP Error: " . $httpCode . "\n"; echo "Response: " . $response; } else { $data = json_decode($response, true); if (json_last_error() === JSON_ERROR_NONE) { // Process JSON data print_r($data); } else { echo "JSON parsing error: " . json_last_error_msg(); echo "Raw response: " . $response; } } }
複製您的 curl 命令 → 貼到輸入框 → 獲取轉換後的 PHP cURL 程式碼
true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_HTTPHEADER => [ "Authorization: Bearer YOUR_TOKEN_HERE", "Content-Type: application/json", "Accept: application/json" ], ]; // First request curl_setopt_array($curl, $options + [ CURLOPT_URL => "https://api.example.com/users", CURLOPT_CUSTOMREQUEST => "GET", ]); $response1 = curl_exec($curl); $err1 = curl_error($curl); // Second request with the same session curl_setopt_array($curl, $options + [ CURLOPT_URL => "https://api.example.com/products", CURLOPT_CUSTOMREQUEST => "GET", ]); $response2 = curl_exec($curl); $err2 = curl_error($curl); curl_close($curl); // Process responses $users = json_decode($response1, true); $products = json_decode($response2, true);
我們的工具處理這些常見的 curl 選項並將它們轉換為適當的 PHP cURL 程式碼:
答:生成的 PHP cURL 程式碼與 PHP 5.5 及以上版本相容。對於較舊的 PHP 版本,可能需要進行小幅調整,特別是用於檔案上傳的 CURLFile 類。
答:是的,生成的程式碼包含基本的 cURL 錯誤處理。對於生產環境的程式碼,您可能需要添加更全面的錯誤處理,以滿足您的應用程式需求。
答:對於 JSON 回應,使用 json_decode() 將回應解析為 PHP 陣列或物件。對於其他格式,您可以根據需要處理原始回應字串。
答:是的,您需要在 PHP 安裝中啟用 cURL 擴展。大多數現代 PHP 安裝默認啟用 cURL。您可以在終端機中使用 php -m | grep curl
進行檢查。
答:對於 PHP 中的檔案上傳,您需要使用 CURLFile 類。我們的轉換器處理帶有 -F
或 --form
選項的 curl 命令,並使用 CURLFile 生成適當的 PHP 程式碼。
答:PHP 的 cURL 擴展提供了 cookie 處理選項。當您轉換包含 cookie 處理的 curl 命令(使用 -b
或 --cookie
)時,我們的工具會生成使用 CURLOPT_COOKIE 或 CURLOPT_COOKIEFILE/CURLOPT_COOKIEJAR 選項正確管理 cookie 的 PHP 程式碼。
答:雖然命令行 curl 非常適合快速 API 測試,但 PHP cURL 允許您將 HTTP 請求直接整合到 PHP 應用程式中。將 curl 轉換為 PHP 有助於縮小 PHP 開發中測試和實現之間的差距。
了解 curl 命令對於使用 PHP 進行有效的 API 測試至關重要。以下是我們的轉換器支援的常見 curl 選項的快速參考:
curl [options] [URL]
-X, --request METHOD
: Specify request method (GET, POST, PUT, DELETE, etc.)-H, --header LINE
: Add header to the request-d, --data DATA
: Send data in POST request-F, --form CONTENT
: Submit form data-u, --user USER:PASSWORD
: Server user and password-k, --insecure
: Allow insecure server connections-I, --head
: Show document info only-v, --verbose
: Make the operation more verbose-s, --silent
: Silent mode--connect-timeout SECONDS
: Maximum time for connection我們的 PHP 轉換器處理複雜的 curl 命令,包括多個標頭、身份驗證、資料負載和各種選項。只需貼上您的 curl 命令,即可獲得使用 cURL 擴展的乾淨、現代的 PHP 程式碼。
使用 PHP cURL 時,請遵循這些最佳實踐,以實現高效且安全的 API 互動:
2. 使用 curl_setopt_array 設置多個選項
"https://api.example.com/data", CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => [ "Authorization: Bearer token123", "Accept: application/json" ], ]); $response = curl_exec($curl); curl_close($curl);3. 實現全面的錯誤處理
"https://api.example.com/data", CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_CUSTOMREQUEST => "GET", ]); $response = curl_exec($curl); $errno = curl_errno($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($errno) { // Handle cURL errors switch ($errno) { case CURLE_OPERATION_TIMEDOUT: echo "Request timed out"; break; case CURLE_COULDNT_CONNECT: echo "Could not connect to server"; break; default: echo "cURL error ({$errno}): " . curl_strerror($errno); } } else if ($httpCode >= 400) { // Handle HTTP errors echo "HTTP error: {$httpCode}"; } else { // Process successful response $data = json_decode($response, true); // Continue processing... }