ScriptCase (v9) Tips

ScriptCase (v9) Tips



Distinguish ScriptCase running environment

We sometimes need to use a different variable for each environment such as production, staging, testing, and development environment. In order to accomplish this, we can use "$_SERVER["SERVER_NAME"]" variable to distinguish the runtime environment.
/**
 * Distinguish runtime environment
 */
if ($_SERVER["SERVER_NAME"] == "scriptcase.example.com") {
	$smtp_host = "smtp1.example.com";
}
else {
	$smtp_host = "smtp2.example.com";
}

Execute SQL SELECT statement in ScriptCase event

   /**
    * Execute SELECT statement
    */
    $username = "foobar";

	$sql = <<<EOD
SELECT COUNT(*) FROM test_table
WHERE 1 = 1
AND username = '{$username}'
EOD;

	sc_lookup(dataset, $sql, "conn_database");

	if ({dataset} === false) {
    	echo "Access error. Message=" . {dataset_erro};
		return false;
	}
	elseif (empty({dataset})) {
	    echo "SELECT command didn't return data.";
		return false;
	}
	else {
    	$cnt = {dataset[0][0]};
		if ($cnt > 0) {
			return false;
		}
	}

Call Web API (REST API) in ScriptCase

    /**
    * Call Web API
    */
    $access_token = "foobar.eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9......";

	$headers = [
		'Authorization' => 'Authorization: Bearer ' . $access_token,
		'Content-Type' => 'Content-Type: application/json'
	];

	$response = sc_webservice("curl", $api_url, 443, "POST", $json, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER=>false, CURLOPT_HTTPHEADER => $headers, CURLOPT_USERAGENT => 'Test APP'), 30, false);


    /**
    * Call Web API
    */
	$response = sc_webservice("curl", $api_url, 80, "POST", $json, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER=>false, CURLOPT_HTTPHEADER => array('Content-Type: application/json',)), 30, false);