PHP code example of cloudfoundry-community / cf-helper-php
1. Go to this page and download the library: Download cloudfoundry-community/cf-helper-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cloudfoundry-community / cf-helper-php example snippets
// Don't do this
$vcap_services = json_decode($_ENV['VCAP_SERVICES']);
use CfCommunity\CfHelper\CfHelper;
$cfHelper = new CfHelper();
$serviceManager = $cfHelper->getServiceManager();
$dbService = $serviceManager->getService('database'); //or regular expression example: getService('.*database.*')
//and for example get the host credential
$host = $dbService->getValue('hostname');//or regular expression example: getValue('ho[A-Za-z]+')
//get all your services
$services = $serviceManager->getAllServices();
//...
$applicationInfo = $cfHelper->getApplicationInfo();
$version = $applicationInfo->getVersion();
$name = $applicationInfo->getName();
$uris = $applicationInfo->getUris();
//for other information contains in VCAP_APPLICATION like limits get with that
$limits = $applicationInfo->limits;
use CfCommunity\CfHelper\CfHelper;
$cfHelper = new CfHelper();
//if we are in cloud foundry we use the connection given by cf-helper-php otherwise we use our database in local
if ($cfHelper->isInCloudFoundry()) {
$db = $cfHelper->getDatabaseConnector()->getConnection();
} else {
$db = new PDO('mysql:host=localhost;dbname=mydbinlocal;charset=utf8', 'root', '');
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//...
$cfHelper->simulateCloudFoundry(); //it use manifest.yml which is in the same folder where this script is called
//to set another manifest.yml:
$cfHelper->simulateCloudFoundry("your_manifest.yml);