PHP code example of nais-standard / sdk

1. Go to this page and download the library: Download nais-standard/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nais-standard / sdk example snippets




ais\Resolver;

$r = (new Resolver())->resolve('weatheragent.nais.id');
if ($r['signature']['verified']) {
    echo $r['card']['mcp'];
    // https://weatheragent.nais.id/mcp
}

echo $r['dns']['parsed']['k'];
// ed25519:oc5a92N1h1Vg9PlnM8CrB0MAw3mMddFhZTrVuMkzceQ

$summary = (new Resolver())->validate('weatheragent.nais.id');
var_dump($summary);

array(16) {
  ["valid"]              => bool(true)
  ["domain"]             => string(22) "weatheragent.nais.id"
  ["version"]            => string(5) "nais1"
  ["manifest_url"]       => string(55) "https://weatheragent.nais.id/.well-known/agent.json"
  ["mcp_endpoint"]       => string(34) "https://weatheragent.nais.id/mcp"
  ["has_mcp"]            => bool(true)
  ["has_card"]           => bool(true)
  ["signature_verified"] => bool(true)
  ["signature_reason"]   => NULL
  ["key"]                => string(50) "ed25519:oc5a92N1h1Vg9PlnM8CrB0MAw3mMddFhZTrVuMkzceQ"
  ["kid"]                => string(50) "ed25519:oc5a92N1h1Vg9PlnM8CrB0MAw3mMddFhZTrVuMkzceQ"
  ["auth"]               => array(1) { [0]=> string(6) "wallet" }
  ["payments"]           => array(1) { [0]=> string(4) "x402" }
  ["pay_to"]             => array(1) { [0]=> "0x742d35Cc6634C0532925a3b8D4C9B7F1A2e3d4E5" }
  ["tags"]               => array(3) { [0]=> "forecast" [1]=> "current_weather" [2]=> "alerts" }
  ["linked_agents"]      => array(1) { [0]=> [ "domain"=>"alerts.weatheragent.nais.id", "relation"=>"partner", "verified"=>true, "name"=>"Severe Weather Alerts" ] }
  ["warnings"]           => array(0) {}
  ["errors"]             => array(0) {}
}

$summary = (new Resolver())->validate('weatheragent.nais.id');
if ($summary['signature_verified']) {
    print_r($summary['tags']);    // ['forecast', 'current_weather', 'alerts']
    print_r($summary['pay_to']);  // pay_to only populated for verified cards
}

$result = \Nais\Resolver::verifyCard($card, 'ed25519:oc5a92N1h1Vg9PlnM8CrB0MAw3mMddFhZTrVuMkzceQ');
var_dump($result['verified'], $result['reason']);

use Nais\Resolver;
use Nais\ResolutionException;

try {
    $result = (new Resolver())->resolve('not-a-real-agent.example.com');
} catch (ResolutionException $e) {
    echo "Failed to resolve {$e->getDomain()}: {$e->getMessage()}";
} catch (\InvalidArgumentException $e) {
    echo "Invalid input: {$e->getMessage()}";
}

$resolver = new \Nais\Resolver([
    'lookupTxt' => fn($name) => ['v=nais1; manifest=https://weatheragent.nais.id/.well-known/agent.json; k=ed25519:...'],
    'fetchCard' => fn($url) => [/* decoded agent.json */],
]);
$r = $resolver->resolve('weatheragent.nais.id');