I am trying to get VM information from VMware vCenter v6.5 with PHP 7. I am receiving error code 400 from curl_getinfo.
I copied the code for this from this post: VCenter ReST API authentication
I've tried this from command line and am able get a session ID, so I know that the server is sending information back as it should, just not to the PHP web page.
Reference for following command: 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);
?>
I expect the output from $out->value
will be a session ID, instead I get NULL
. Grateful for any help, thanks!
my best guess is that VCenter blocks requests with no user-agent header, and curl-cli adds such a header automatically, but libcurl / php's libcurl wrappers does not. try
curl_setopt($ch,CURLOPT_USERAGENT, 'php/' . PHP_VERSION . ' libcurl/' . (curl_version()['version']));
then you'll get something like
User-Agent: php/7.1.16 libcurl/7.59.0
which is truthful :)
The http 400 error 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');
With curl you can connect in this way:
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**
Then you get as response:
{
"value": "vmware-api-session-id"
}
With this id you can do:
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": {
}
}'
and get
{
"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"
}
]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.