使用PHP连接到vCenter REST API

时间:2019-01-16 16:58:17

标签: php rest curl vmware vcenter

我正在尝试使用PHP 7从VMware vCenter v6.5获取VM信息。我从curl_getinfo收到错误代码400。

我从这篇文章中复制了此代码:VCenter ReST API authentication

我已经从命令行尝试了此操作,并且能够获取会话ID,所以我知道服务器正在按原样发送信息,只是没有发送到PHP网页。

以下命令的参考:https://communities.vmware.com/thread/556377

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'vmware-use-header-authn: test' --header 'vmware-api-session-id: null' -u 'administrator@vsphere.local' 'https://vcenter.mydomain.local/rest/com/vmware/cis/session'
<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,'https://vcenter.mydomain.local/rest/com/vmware/cis/session');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator@vsphere.local:Passw0rd');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test',
'vmware-api-session-id: null',
'Expect:'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

$out = json_decode(curl_exec($ch));

if(!curl_exec($ch)){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

$info = curl_getinfo($ch);
echo "<p>CURL URL: " . $info['url'];
echo "<p><font color=green>CURL Dump: <br>";
echo '<pre>';
var_dump($info);
echo "</pre>\n";
echo "<p>OUT:<br>";
var_dump($out);
if ($out === false) {
   echo 'Curl Error: ' . curl_error($ch);
   exit;
}
$sid = $out->value;

echo "<br>SID: " . $sid;

curl_close($ch);
?>

我希望$out->value的输出将是一个会话ID,而不是NULL。感谢您的帮助,谢谢!

3 个答案:

答案 0 :(得分:0)

我的最佳猜测是,VCenter会阻止没有用户代理标头的请求,而curl-cli会自动添加这样的标头,但libcurl / php的libcurl包装器却不会。尝试

curl_setopt($ch,CURLOPT_USERAGENT, 'php/' . PHP_VERSION . ' libcurl/' . (curl_version()['version']));

那么你会得到类似的东西

User-Agent: php/7.1.16 libcurl/7.59.0

这是真实的:)

答案 1 :(得分:0)

使用curl可以通过以下方式进行连接:

curl -X POST \
  https://<your_vcenter>/rest/com/vmware/cis/session \
  -H 'Accept: application/json' \
  -H 'Authorization: Basic <encoded_password...see *1)' \
  -H 'Content-Type: application/json' \
  -H 'vmware-use-header-authn: SomerandomValue'

*1 => https://en.wikipedia.org/wiki/Basic_access_authentication**

然后您得到答复:

{
    "value": "vmware-api-session-id"
}

使用此ID,您可以执行以下操作:

curl -X GET \
  https://<your_vcenter>/rest/vcenter/host \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'vmware-api-session-id: vmware-api-session-id' \
  -d '{
    "filter": {
    }
}'

并获得

{
    "value": [
        {
            "host": "host-1",
            "name": "esx01.your.domain",
            "connection_state": "CONNECTED",
            "power_state": "POWERED_ON"
        },
        {
            "host": "host-2",
            "name": "esx02.your.domain",
            "connection_state": "CONNECTED",
            "power_state": "POWERED_ON"
        }
    ]
}

答案 2 :(得分:0)

http 400 错误是由这个头引起的:

curl_setopt($ch, CURLOPT_POST, true);

用这个替换它可以解决问题:

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