PHP code example of switcher-io / switcher-php

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

    

switcher-io / switcher-php example snippets


$urlId = 'url identifier of the switch, e.g. "abc123" in https://dmsr.io/abc123.';
$key = 'switch key';

//initialize the api
$sw = new \SwitcherIO\DeadManSwitch($urlId, $key);

//call the /start endpoint to signal your job started (optional - only used if your switch has a max run time set)
$sw->start();

//your job code goes here

//call /complete to notifiy Switcher.io the job has finished
$sw->complete();

//you can also pause the switch
$sw->pause();

use \SwitcherIO\DeadManSwitch;

$sw = new DeadManSwitch('url id', 'key');

try {
    $sw->start();
} catch (\SwitcherIO\SwitcherException $e) {

    if ($e->getStatusCode() === DeadManSwitch::STATUS_ERROR_404) {
        //oops, you either got the url id or key wrong...
    } else if ($e->getStatusCode() === DeadManSwitch::STATUS_ERROR_START_BEFORE_COMPLETE) {
        /*
         * You get this error if your switch is using a max runtime, and for some reason your job
         * starts a new run before the last run finished. If this is a problem for you, handle it here...
         */
    }

}


//in this case complete() act as if it ran succesfully, and will not actually ping a switcher.io url
$sw = new \SwitcherIO\DeadManSwitch('test', 'key-does-not-matter-for-test-url');
$sw->complete(); 

/*
 * In this case complete() will throw a \SwitcherIO\SwitcherException with a status code 
 * of \SwitcherIO\DeadManSwitch::STATUS_TEST_ERROR
 */
$sw = new \SwitcherIO\DeadManSwitch('test-error', 'key-does-not-matter-for-test-url');
$sw->complete();