Both IBM Cloud Foundry and IBM Cloud Code Engine use a JSON-encoded environment variable to expose service bindings: VCAP_SERVICES
for IBM Cloud Foundry and CE_SERVICES
for IBM Cloud Code Engine. These variables provide important information, such as access credentials, API endpoints, key names and identifiers, which your application will need to connect to the corresponding services.
If you’re using PHP, you can access this variable using the getenv()
function. You can then decode it into either an associative array or an object and use it to initialize the corresponding service client.
The following code demonstrates both approaches:
<?php
if (getenv("CE_SERVICES") === false) {
die("Variable CE_SERVICES not set");
}
$services_json = getenv("CE_SERVICES");
// decode into object
$services = json_decode($services_json);
print_r($services->{'cloudantNoSQLDB'}[0]->credentials->url);
// decode into array
$services = json_decode($services_json, true);
print_r($services['cloudantNoSQLDB'][0]['credentials']['url']);
?>