PHP code example of jalle19 / haphproxy

1. Go to this page and download the library: Download jalle19/haphproxy 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/ */

    

jalle19 / haphproxy example snippets


// Create a parser
try {
	$parser = new Parser('/etc/haproxy/haproxy.cfg');
} catch (FileNotFoundException $e) {
	die($e->getMessage());
}

// Parse and dump the configuration
$configuration = $parser->parse();
$writer = new Writer($configuration);

echo $writer->dump();

$configuration = new Configuration();

// Add a global section
$globalSection = new Section\GlobalSection();
$globalSection->addParameter(new Parameter('daemon'))
              ->addParameter(new Parameter('maxconns', 128));
$configuration->addSection($globalSection);

// Add a defaults section
$defaultsSection = new Section\DefaultsSection();
$defaultsSection->addParameter(new Parameter('mode', 'http'));
$configuration->addSection($defaultsSection);

// Dump the configuration
$writer = new Writer($configuration);

echo $writer->dump();


$configuration = new Configuration();
$writer = new Writer($configuration);

// Remember to ho $writer->dump();


// Make a section with some parameters
$section = new Section\DefaultsSection();
$section->addParameter(new Parameter('mode', 'http'));
$section->addParameter(new Parameter('timeout', 'client 30s'));
$section->addParameter(new Parameter('timeout', 'connect 30s'));
$section->addParameter(new Parameter('timeout', 'server 30s'));

// Get the value of a single parameter
$modeParameter = $section->getParameter('mode');
$mode = $modeParameter->getValue();

// Loop through all the "timeout" parameters
foreach ($section->getParametersByName('timeout') as $timeoutParameter) {

}

$configuration = new Configuration();
$configuration->addSection(new FrontendSection('frontend frontend-1'));
$configuration->addSection(new FrontendSection('frontend frontend-2'));
$configuration->addSection(new FrontendSection('frontend frontend-3'));

foreach ($configuration->getFrontendSections() as $frontendSection) {

}

$configuration = new Configuration();

$section = new Section\DefaultsSection();
$section->addMagicComment('magic');
$section->addParameter(new Parameter('mode', 'http'));

$writer = new Writer($configuration);

echo $writer->dump();