Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Bu kodun işlevselliğini hiçbir şekilde bozmadan yeni özellikler eklemeden sadece daha…

  1. User

    Bu kodun işlevselliğini hiçbir şekilde bozmadan yeni özellikler eklemeden sadece daha optimize hale getir

  2. Assistant
    <?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    
    $apiUrl = "https://www.bloomberght.com/chart/ekonomi/doviz";
    
    $ch = curl_init($apiUrl);
    
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 10,
    ]);
    
    $response = curl_exec($ch);
    
    if ($response === false) {
        http_response_code(500);
        echo json_encode(["error" => "cURL hatası: " . curl_error($ch)]);
        curl_close($ch);
        exit;
    }
    
    $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpStatus !== 200) {
        http_response_code($httpStatus);
        echo json_encode(["error" => "Kaynak API'den beklenmeyen HTTP durumu: " . $httpStatus]);
        exit;
    }
    
    $data = json_decode($response, true);
    
    if (json_last_error() !== JSON_ERROR_NONE) {
        http_response_code(500);
        echo json_encode([
            "error" => "Kaynak API'den alınan veri geçerli bir JSON değil.",
            "json_error" => json_last_error_msg()
        ]);
        exit;
    }
    
    if (!isset($data['body']['foreignCurrency'])) {
        http_response_code(500);
        echo json_encode(["error" => "'foreignCurrency' verisi 'body' anahtarının altında bulunamadı."]);
        exit;
    }
    
    $foreignCurrency = $data['body']['foreignCurrency'];
    
    $fieldsToKeep = [
        "type",
        "currency",
        "name",
        "secName",
        "longName",
        "code",
        "symbol",
        "ibsSembol",
        "currencySymbol",
        "toCurrencySymbol",
        "percentSymbol",
        "slug",
        "lastPrice",
        "buying",
        "sales",
        "lastUpdateTime"
    ];
    
    if (isset($foreignCurrency['items']) && is_array($foreignCurrency['items'])) {
        $foreignCurrency['items'] = array_map(function ($currency) use ($fieldsToKeep) {
            return array_intersect_key($currency, array_flip($fieldsToKeep));
        }, $foreignCurrency['items']);
    } else {
        http_response_code(500);
        echo json_encode([
            "error" => "'foreignCurrency' içindeki 'items' anahtarı bulunamadı veya geçerli bir dizi değil."
        ]);
        exit;
    }
    
    $filteredData = [
        "foreignCurrency" => array_intersect_key(
            $foreignCurrency,
            array_flip(["type", "name", "nameTr", "nameStatus", "items"])
        )
    ];
    
    echo json_encode($filteredData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    exit;
    ?>