Read Time: 1 Min

The following is a method for utilising NFT’s within your website, opposed to art/graphics/stock.

In this example, we use the OpenSea API to fetch the metadata of the NFT based on the contract address and token ID. We then parse the JSON response to extract the image URL of the NFT. Finally, we display the NFT image on the website using an <img> tag.

Please note that you’ll need to replace the placeholder values for the contract address and token ID with the actual values corresponding to the NFT you want to display. Additionally, ensure that the server running the PHP code has access to the file_get_contents function and external HTTP requests are not blocked.

				
					<?php
// Specify the contract address and the token ID of the NFT
$contractAddress = '0x123456789abcdef'; // Replace with the actual contract address
$tokenId = '123'; // Replace with the actual token ID

// API endpoint for fetching the metadata of the NFT
$apiEndpoint = "https://api.opensea.io/api/v1/asset/{$contractAddress}/{$tokenId}";

// Fetch the metadata of the NFT
$response = file_get_contents($apiEndpoint);
if ($response === false) {
    // Handle error if unable to fetch metadata
    echo "Error fetching NFT metadata";
    exit;
}

// Parse the JSON response
$nftData = json_decode($response, true);

// Get the image URL of the NFT
$imageUrl = $nftData['image_url'];

// Display the NFT image on the website
echo "<img src=\"{$imageUrl}\" alt=\"NFT Image\">";

// You can also access other metadata of the NFT, such as name and description, using:
// $nftName = $nftData['name'];
// $nftDescription = $nftData['description'];
// echo "<h1>{$nftName}</h1>";
// echo "<p>{$nftDescription}</p>";
?>