2

I'm trying to use the vCloud director API for the first time using PHP. This is the first 2 steps in the documentation - https://pubs.vmware.com/vca/index.jsp#com.vmware.vcloud.api.doc_56/GUID-6DC15CF5-3BCF-4426-9988-C71E7A71CBD6.html

  1. Make an API versions request to vCloud Director to obtain the login URL for the REST API.

  2. Use the login URL to create a login session. POST a request to this URL that includes your username, password, and organization name in a MIME Base64 encoding.

However, I can't create a login session using the login information I use in logging in to the vCloud GUI.

Below is my code:

//step 1
$url = 'https://vcloud.test.com/api/versions';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);

curl_close($curl);

$oXML = new SimpleXMLElement($result);
$loginURL = null;
foreach($oXML->VersionInfo as $oEntry){
    $loginURL = $oEntry->LoginUrl;
    break;
}

//step 2
$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_POST, 1);

$loginData = 'api@System:password_here';
$data = base64_encode($loginData);
curl_setopt($curl2, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl2, CURLOPT_URL, $loginURL);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);

$result2 = curl_exec($curl2);
curl_close($curl2);

var_dump($result2);

The $result2 only returns false.

Any inputs will be greatly appreciated.

Thanks.

japhfortin
  • 361
  • 4
  • 6
  • 18
  • Did you make sure to set the `Accept` and `Content-Type` headers? You'll want something like `application/*+xml;version=30.0` for both (e.g. for API Version 30) – frostmatthew May 14 '18 at 12:01
  • Did you solve this, I have the same issue, I can do curl from cmdline and I'll get the session id... I can't do it from php as I get Null ( even added curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . "application/xml" )); ) and did not help – VladoPortos Sep 28 '20 at 13:39

0 Answers0