使用REST API认证错误,使用PHP连接到VCenter。

问题描述 投票:1回答:1

我按照vsphere官方网站上的说明,从服务器上获取信息,并从回答的 另一位用户 .根据我的理解,首先我必须获取会话ID(cis id),但我得到的结果是 "null"。

我尝试了多个代码,但结果都是一样的。

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

结果都是空的

php api vmware vsphere vcenter
1个回答
0
投票

当你调试你的代码时,$out变量返回null吗?如果是,你可以检查一下你的curl返回的是什么?56 - OpenSSL SSL_read: 成功错误$out = json_decode(curl_exec($ch));?

我的代码出现了这个错误,我看到7.67版本的cURL造成了这个问题。

我的解决办法是把cURL的版本降到7.65。

这对我来说是可行的

© www.soinside.com 2019 - 2024. All rights reserved.