Last Updated: February 25, 2016
· zeroecco

Shim your PHP Fog env_vars into AppFog

The below code is what I use to shim AppFog VCAP_SERVICES into PHP Fog Environmental Variables (this only works with PHP apps). To accomplish this, Follow these steps:

Create phpfog.php file and place it in the same directory as your frameworks database configuration file.

Paste this code into the phpfog.php file:

$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services,true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];    
putenv('MYSQL_DB_HOST=' . $mysql_config["hostname"] . ':'  . $mysql_config["port"] );
putenv('MYSQL_USERNAME='. $mysql_config["user"]);
putenv('MYSQL_PASSWORD='. $mysql_config["password"]);
putenv('MYSQL_DB_NAME='. $mysql_config["name"]);

In your configuration file add this line right below the "<?php":
include 'phpfog.php';

This will shim the VCAP_SERVICES environmental variable in AppFog and other Cloud Foundry distributions into standard Environmental Variables.