2

I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here . From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.

I tried multiple codes but the result is the same:

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $headers = array(
            'Content-Type: application/json',
            'Accept: application/json',
            'vmware-use-header-authn: test'
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator@vs.dom:userpassword');
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );

$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
    echo 'Curl Error: ' . curl_error($ch);
    exit;
}
$sid = $out;
        dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");

$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);

curl_close($ch);

The result is null.

I'm the man.
  • 207
  • 2
  • 13
alessiow
  • 29
  • 6

3 Answers3

1

this is a script word 100% using PHP

    <?php
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');

    $out = json_decode(curl_exec($ch));
    // var_dump($out);
    if ($out === false) {
      echo 'Curl Error: ' . curl_error($ch);
      exit;
    }
    $sid = $out->value;

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");

    $output = curl_exec($ch);
    $vms = json_decode($output);
    var_dump($vms);

    curl_close($ch);

    ?>
carmel
  • 902
  • 7
  • 24
zakaria
  • 11
  • 1
0

when you debug your code, the $out variable return null? If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?

This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.

My solution was to downgrade cURL version to 7.65.

It works for me!

0

I had the same problem, and eventually understood that it is caused by this header:

curl_setopt($ch, CURLOPT_POST, true);

And replacing it with this one solves the issue:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
Arie Rave
  • 11
  • 1