alessiow
alessiow

Reputation: 29

Connecting to VCenter with PHP using REST API authentication error

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, '[email protected]: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.

Upvotes: 2

Views: 1091

Answers (3)

zakaria
zakaria

Reputation: 11

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);

    ?>

Upvotes: 1

Arie Rave
Arie Rave

Reputation: 11

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');

Upvotes: 0

Anselmo Junior
Anselmo Junior

Reputation: 1

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!

Upvotes: 0

Related Questions