Sample PHP code 101

This is a PHP code sample to use the get Top 100 Holders API.

You can usually just modify the $url value to use the other API functions.

For example, you can modify the $url value to "https://agent1.xyz/codes/getHints/s1" to use Read Hints from Code Hunter API.

// Use getTop100Agent1Holders() to get a list of Top 100 Holders.

function getTop100Agent1Holders() {

    // API: https://a1api.moonlanders.wiki/reference/api-reference/holders-and-wallets
    // Modify to use other API.
    $url = "https://agent1.xyz/nft/getStats/top100holders";
    
    // Enter your AGENT1 X-API-KEY (if any or leave blank "")
    $apiKey = "10001"; 
    
    // Enter your AGENT1 X-API-NAME (if any or leave blank "")
    $appName = "AGENT1_MADE_ME_A_CODER"; 
    try {
        $json_data = fetchJsonData($url, $apiKey, $appName);
        print_r($json_data);
    } catch (Exception $e) {
        echo 'Error: ' . $e->getMessage();
    }
	
}

function fetchJsonData($url, $apiKey = "", $appName = "") {
    // Initialize cURL session
    $curl = curl_init($url);

    // Set the custom headers
    $headers = array(
        'X-API-KEY: ' . $apiKey,
        'X-APP-NAME: ' . $appName
    );

    // Set cURL options
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    // Execute cURL session and get the JSON data as a string
    $json_string = curl_exec($curl);

    // Check if the operation was successful
    if ($json_string === false) {
        $error = curl_error($curl);
        curl_close($curl);
        throw new Exception('Error fetching JSON data from ' . $url . ': ' . $error);
    }

    // Close cURL session
    curl_close($curl);

    // Decode the JSON string into a PHP associative array
    $json_data = json_decode($json_string, true);

    // Check if the JSON decoding was successful
    if ($json_data === null && json_last_error() !== JSON_ERROR_NONE) {
        throw new Exception('Error decoding JSON data: ' . json_last_error_msg());
    }

    return $json_data;
}

Last updated