[英]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 .我按照 vsphere 官方站点中的说明从服务器和此处另一个用户的回答中获取信息。 From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
据我了解,首先我必须获得 session id(cis id),但结果是“null”。
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.结果是 null。
when you debug your code, the $out variable return null?当您调试代码时,$out 变量返回 null? If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in
$out = json_decode(curl_exec($ch));
如果是,您能否检查您的 curl 是否返回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.这个错误出现在我的代码中,我看到 7.67 版本中的 cURL 导致了这个问题。
My solution was to downgrade cURL version to 7.65.我的解决方案是将 cURL 版本降级到 7.65。
It works for me!这个对我有用!
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');
this is a script word 100% using PHP这是一个 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.