使用 REST API 身份验证错误使用 PHP 连接到 VCenter [英] Connecting to VCenter with PHP using REST API authentication error

查看:39
本文介绍了使用 REST API 身份验证错误使用 PHP 连接到 VCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照官方 vsphere 站点中的说明从服务器和另一个用户的回答中获取信息.据我了解,首先我必须获得会话 ID(cis id),但结果我得到了null".

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

结果为空.

推荐答案

我也遇到了同样的问题,最终明白是这个header引起的:

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

这篇关于使用 REST API 身份验证错误使用 PHP 连接到 VCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆