cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
724
Views
0
Helpful
1
Replies

How can I authenticate to Intersight API's using PHP

dcuser
Level 1
Level 1

How can I authenticate to Intersight API's using PHP?  I am trying to pull inventory and alarm information but cannot authenticate

1 Reply 1

Brian Morrissey
Cisco Employee
Cisco Employee

I have this php example, you would substitute $apikey with your api key id from Intersight and change the path in $pkeyid to your secret key file.

The $url in the example pulls 1 entry from /api/v1/compute/RackUnits and outputs it in JSON

<?php
$apikey = 'xxxx/yyy/zzz';
$method = 'GET';
$hostName = 'intersight.com';
$url = '/api/v1/compute/RackUnits?$top=1&$select=Serial,Name';

$apiTime = 'date: ' . gmdate(DATE_RFC1123);

$payload = '';
$payloadDigest = openssl_digest ($payload , "sha256", true);
$payloadDigest = 'digest: SHA-256=' . base64_encode($payloadDigest);

$apiSignature = '(request-target): ' . strtolower($method) . ' ' . strtolower($url);
$apiSignature .= PHP_EOL . $apiTime;
$apiSignature .= PHP_EOL . $payloadDigest;
$apiSignature .= PHP_EOL . 'host: ' . $hostName;

$pkeyid = openssl_pkey_get_private("file:///mnt/c/SecretKey.txt");

openssl_sign($apiSignature, $signature, $pkeyid, "sha256WithRSAEncryption");
openssl_free_key($pkeyid);

$apiSignature = base64_encode($signature);

// Create a stream
$opts = array(
'http'=>array(
'protocol_version'=>"1.1",
'ignore_errors'=>true,
'method'=>$method,
'header'=>["Accept: application/json",
"Host: " . $hostName,
$apiTime,
$payloadDigest,
"Authorization: Signature keyId=\"" . $apikey . "\",algorithm=\"rsa-sha256\",headers=\"(request-target) date digest host\",signature=\"" . $apiSignature . "\""]
)
);

$context = stream_context_create($opts);

$url = 'https://' . $hostName . $url;

$output = file_get_contents($url, false, $context);
echo $output;

?>

 

Example Output:

{
  "ObjectType": "compute.RackUnit.List",
  "Results": [
    {
      "ClassId": "compute.RackUnit",
      "Moid": "xxxxxx",
      "Name": " C220-server1",
      "ObjectType": "compute.RackUnit",
      "Serial": "WZP123456"
    }
  ]
}

Review Cisco Networking for a $25 gift card