Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Form CURL JSON</title>
- </head>
- <body>
- <h1>Submit URL untuk Mendapatkan JSON</h1>
- <form method="POST" action="">
- <label for="url">Masukkan URL JSON:</label><br>
- <input type="url" id="url" name="url" required placeholder="https://example.com/data.json"><br><br>
- <button type="submit">Submit</button>
- </form>
- <?php
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) {
- $url = filter_var($_POST['url'], FILTER_SANITIZE_URL);
- // Validasi URL
- if (filter_var($url, FILTER_VALIDATE_URL)) {
- // Inisialisasi cURL
- $ch = curl_init();
- // Konfigurasi cURL
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // Eksekusi cURL
- $response = curl_exec($ch);
- // Cek error
- if (curl_errno($ch)) {
- echo "<p>cURL Error: " . curl_error($ch) . "</p>";
- } else {
- // Decode JSON
- $data = json_decode($response, true);
- if (json_last_error() === JSON_ERROR_NONE) {
- echo "<h2>Hasil JSON:</h2>";
- echo "<pre>" . htmlspecialchars(json_encode($data, JSON_PRETTY_PRINT)) . "</pre>";
- } else {
- echo "<p>JSON Error: " . json_last_error_msg() . "</p>";
- }
- }
- // Tutup cURL
- curl_close($ch);
- } else {
- echo "<p>URL tidak valid. Silakan masukkan URL yang benar.</p>";
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement