PHP code example of serato / slimulator

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

    

serato / slimulator example snippets


use Serato\Slimulator\EnvironmentBuilder;

$envBuilder = EnvironmentBuilder::create()
	->setRequestMethod('GET')
	->setUri('http://my.server/my/uri?var1=val1')
	->addGetParam('var2', 'val2')
	->addHeader('Cache-Control', 'no-cache')
	->addCookie('my_session', 'session_vars');


use Serato\Slimulator\EnvironmentBuilder;

$envBuilder = EnvironmentBuilder::create()->setUri('http://my.server/my/uri?var1=val1');

$server = $envBuilder->getEnv();


use Serato\Slimulator\EnvironmentBuilder;

$envBuilder = EnvironmentBuilder::create()->setUri('http://my.server/my/uri?var1=val1');

$env = $envBuilder->getSlimEnvironment();


use Serato\Slimulator\EnvironmentBuilder;
use Serato\Slimulator\RequestBody\UrlEncoded;
use Serato\Slimulator\RequestBody\Multipart;
use Serato\Slimulator\RequestBody\Json;
use Serato\Slimulator\RequestBody\Xml;

// Create a request body using `application/x-www-form-urlencoded` encoding
$body = UrlEncoded::create(['var1' => 'val1', 'var2' => 'val2']);

// Create a request body with an `application/json` content type
$body = Json::create(['var1' => 'val1', 'var2' => 'val2']);
// Can also be created from a JSON string
$body = Json::create('{"var1":"val1","var2":"val2"}');

// Create a request body with an `application/xml` content type
$body = Xml::create('<xml><var1>val1</var1><var2>val2</var2></xml>');

// Create a multipart request body
$body = Multipart::create()
	->addParam('var1', 'val1') // Add a name/value pair
	->addFile('file1', '/my/local/file/path'); // Add a file

// Add the body to a request environment
$envBuilder = EnvironmentBuilder::create()
	->setRequestMethod('POST')
	->setUri('http://my.server/my/uri')
	->setRequestBody($body);


use Serato\Slimulator\EnvironmentBuilder;
use Serato\Slimulator\Authorization\BasicAuthorization;
use Serato\Slimulator\Authorization\BearerToken;

// Create a request environment that uses `Basic` authorization
$envBuilder = EnvironmentBuilder::create()
	->setUri('http://my.server/my/uri')
	->setAuthorization(BasicAuthorization::create('myuser', 'mypass'));

// Create a request environment that uses a `Bearer` token
$envBuilder = EnvironmentBuilder::create()
	->setUri('http://my.server/my/uri')
	->setAuthorization(BearerToken::create('mytoken'));


use Serato\Slimulator\EnvironmentBuilder;
use Serato\Slimulator\Request;

$envBuilder = EnvironmentBuilder::create()->setUri('http://my.server/my/uri');
$request = Request::createFromEnvironmentBuilder($envBuilder);


use Slim\App;
use Serato\Slimulator\EnvironmentBuilder;
use Serato\Slimulator\Request;

// Create the app...
$app = new App();
// ...and get the DI container
$container = $app->getContainer();

// Create an EnvironmentBuilder in the container
$container['environmentBuilder'] = function () {
	return EnvironmentBuilder::create()->setUri('http://my.server/my/uri');
}

// Replace the default `environment` in the container with our constructed
// environment created out of the EnvironmentBuilder instance
$container['environment'] = function ($c) {
	return $c->get('environmentBuilder')->getSlimEnvironment();
};

// And do the same for the container's default `request` object
$container['request'] = function ($c) {
	return Request::createFromEnvironmentBuilder(
		$c->get('environmentBuilder')
	);
};

// Add routes, middleware, handlers etc
// ...

// Get the `response` object by calling App::run with the `$silent` argument set to `true`.
$response = $app->run(true);