【问题标题】:Connection to vCenter REST API with PHP使用 PHP 连接到 vCenter REST API
【发布时间】:2019-06-10 19:44:59
【问题描述】:

我正在尝试使用 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-&gt;value 的输出将是一个会话 ID,而不是我得到 NULL。感谢您的帮助,谢谢!

【问题讨论】:

  • 你解决过这个问题吗?
  • @VladoPortos - 是的,我能够让它完美地工作。我相信这是由于我使用的标头有问题。
  • 同样我得到它的工作......现在我正在尝试从 /mob 请求票证的问题 ...method=acquireTicket 它失败了“可能的 XSRF(跨站点请求伪造)检测到”,我无法通过 :(

标签: php rest curl vmware vcenter


【解决方案1】:

我最好的猜测是 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

这是真实的:)

【讨论】:

  • 谢谢hanshenrik。我已经添加了它,它现在确实在标题中显示了 User-Agent: php/7.2.14 libcurl/7.29.0 ,但仍然没有得到很好的响应。粘贴 curl_dump 以防万一:pastebin.com/x2ZFSasp
  • @SuperDogStar 嗯...如果您将--verbose 添加到 curl 命令,您会得到什么标题?
  • 问题可能与标题中的“Content-Length:-1”有关吗?还有一条额外的线,我不确定它来自哪里。 curl_dump 从 PHP 代码粘贴到这里:pastebin.com/x2ZFSasp
  • 好的,详细运行。我还必须在最后添加 --insecure 到命令行以绕过 SSL 检查,忘了在上面添加。在 curl 命令行上使用 --verbose 的结果可以在这里找到:pastebin.com/pswSYk3b
【解决方案2】:

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

curl_setopt($ch, CURLOPT_POST, true);

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

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

【讨论】:

    【解决方案3】:

    使用 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"
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2020-04-19
      • 2013-07-26
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2017-12-15
      • 2019-10-09
      • 2019-06-10
      相关资源
      最近更新 更多