I-convert ang mga curl command sa PHP code - Gumawa ng ready-to-use na PHP cURL code para sa mga API request
"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; }
Narito ang ilang karaniwang curl command na maaari mong i-convert sa PHP code:
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
Ang cURL extension ng PHP ay nagbibigay ng makapangyarihang paraan para gumawa ng mga HTTP request. Narito ang ilang karaniwang PHP cURL pattern:
"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; } } }
Kopyahin ang iyong curl command → I-paste sa input box → Kunin ang na-convert na PHP cURL code
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);
Ang aming tool ay nagha-handle ng mga karaniwang curl option na ito at kino-convert ang mga ito sa angkop na PHP cURL code:
S: Ang na-generate na PHP cURL code ay compatible sa PHP 5.5 at mas bago. Para sa mas lumang mga PHP version, maaaring kailanganin ang mga minor adjustment, lalo na para sa CURLFile class na ginagamit sa file upload.
S: Oo, ang na-generate na code ay may kasamang basic error handling para sa mga cURL error. Para sa production code, maaaring gusto mong magdagdag ng mas komprehensibong error handling na specific sa mga pangangailangan ng iyong application.
S: Para sa mga JSON response, gamitin ang json_decode() para i-parse ang response sa isang PHP array o object. Para sa ibang mga format, maaari mong i-process ang raw response string ayon sa pangangailangan.
S: Oo, kailangan mo ng cURL extension na naka-enable sa iyong PHP installation. Karamihan ng mga modernong PHP installation ay may cURL na naka-enable by default. Maaari mong i-check gamit ang php -m | grep curl
sa iyong terminal.
S: Para sa file upload sa PHP, kailangan mong gamitin ang CURLFile class. Ang aming converter ay nagha-handle ng mga curl command na may -F
o --form
option at gumagawa ng angkop na PHP code gamit ang CURLFile.
S: Ang cURL extension ng PHP ay nagbibigay ng mga option para sa cookie handling. Kapag nag-convert ka ng mga curl command na may kasamang cookie handling (gamit ang -b
o --cookie
), ang aming tool ay gumagawa ng PHP code na maayos na namamahala ng mga cookie gamit ang CURLOPT_COOKIE o CURLOPT_COOKIEFILE/CURLOPT_COOKIEJAR option.
S: Habang ang command-line curl ay mahusay para sa mabilis na API testing, ang PHP cURL ay nagbibigay-daan sa iyo na direktang i-integrate ang mga HTTP request sa iyong mga PHP application. Ang pag-convert ng curl sa PHP ay tumutulong na tulay ang pagitan ng testing at implementation sa PHP development.
Ang pag-unawa sa mga curl command ay mahalaga para sa epektibong API testing gamit ang PHP. Narito ang isang mabilis na reference ng mga karaniwang curl option na sinusuportahan ng aming converter:
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 connectionAng aming PHP converter ay nagha-handle ng mga kumplikadong curl command kabilang ang maraming header, authentication, data payload, at iba't ibang option. I-paste lang ang iyong curl command at makakuha ng malinis, modernong PHP code gamit ang cURL extension.
Kapag gumagamit ng PHP cURL, sundin ang mga best practice na ito para sa mahusay at secure na API interaction:
2. Gumamit ng curl_setopt_array para sa Maraming Option
"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. Mag-implement ng Komprehensibong Error Handling
"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... }