簡體   English   中英

使用 PHP 使用 REST API 身份驗證錯誤連接到 VCenter

[英]Connecting to VCenter with PHP using REST API authentication error

我按照 vsphere 官方站點中的說明從服務器和此處另一個用戶的回答中獲取信息。 據我了解,首先我必須獲得 session 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, '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);

結果是 null。

當您調試代碼時,$out 變量返回 null? 如果是,您能否檢查您的 curl 是否返回56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch)); ?

這個錯誤出現在我的代碼中,我看到 7.67 版本中的 cURL 導致了這個問題。

我的解決方案是將 cURL 版本降級到 7.65。

這個對我有用!

我有同樣的問題,最終明白是由這個標題引起的:

curl_setopt($ch, CURLOPT_POST, true);

用這個替換它解決了這個問題:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

這是一個 100% 使用 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); ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM