PHP code example of kengoldfarb / underscore_libs

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

    

kengoldfarb / underscore_libs example snippets


use _\_Log;
_Log::$logLevel = _\_LogContants::INFO;

_Log::debug('This is a debug message that will not be written because of log level');
_Log::info('This is an info message');
_Log::warn('This is a warning message');
_Log::crit('This is a critical message');
_Log::fatal('Oh snap.  This is a fatal error message');

use _\_Crypt;

// Set the secret key
$secretKey = "MySecretKey123";

// The text to encrypt
$textToEncrypt = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";

// The encrypted text
$encryptedText = _Crypt::_encryptAESPKCS7($textToEncrypt, $secretKey);

// The decrypted text (will match $textToEncrypt)
$decryptedText = _Crypt::_decryptAESPKCS7($encryptedText, $secretKey);

$keys = _Crypt::_generateRSAKeys();
$publicKey = $keys['public'];
$privateKey = $keys['private'];

$textToEncrypt = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
$encryptedText = _Crypt::_encryptRSA($textToEncrypt, $publicKey);
$decryptedText = _Crypt::_decryptRSA($encryptedText, $privateKey);

use _\_UUID;

$uuid = _UUID::getUUID();

use _\_UUID;

$uuid = '55a0afe6-0e00-4c08-9fb9-7905f0a106b6';
$binaryUUID = _UUID::charUUIDToBinary($uuid);

use _\_UUID;

$uuid = '55a0afe6-0e00-4c08-9fb9-7905f0a106b6';
$binaryUUID = _UUID::charUUIDToBinary($uuid);

$strUUID = _UUID::binaryUUIDToCharUUID($binaryUUID);

// At this point, $uuid == $strUUID

use _\_Db;
$host = 'localhost';
$user = 'root';
$pass = 'mysupersecretpassword';
$dbName = 'the_db';
$port = 3306;

_Db::_createConnection($host, $username, $password, $dbName, $port);

$sql = "select * from users";
$sql = _Db::_escape($sql);
$queryResult = _Db::_query($sql);

if($queryResult === FALSE) {
	// Handle error
}else{
	$numRows = _Db::_count();
	while($row = _Db::getRow()) {
		// Do something with $row
	}
}

$sql = "insert into users (name, username) values ('Ken', 'ken')";
$queryResult = _Db::_query($sql);
if($queryResult === FALSE) {
	// Handle error
}else{
	$userId = _Db::_lastId();
}

use _\_File;
$fileData = _File::_readAllFromFile('/tmp/myfile.txt');
if($fileData === FALSE) {
	// Handle error reading from file
}

echo $fileData;

use _\_File;
$writeSuccessful = _File::_writeToFile('Here is some text for myfile.txt', '/tmp/myfile.txt');
if($writeSuccessful) {
	echo $fileData;
}else{
	// Hand error writing file
}

use _\_Info;
$userIp = _Info::_getUserIpAddr();

use _\_Info;
$mysqlDatetime = _Info::_mysqlNow();

use _\_Rand;
$min = 0;
$max = 100;
_Rand::_getRand($min, $max);

use _\_Rand;
$length = 10; // The number of characters in the string
$lettersNumbersOnly = true; // String will contain only letters and numbers
_Rand::_randString($length, $lettersNumbersOnly);

use _\_ServiceResponse;
_ServiceResponse::_success(array('hello' => 'world'));

// Result: {"status": "success", "hello": "world"}

use _\_ServiceResponse;
_ServiceResponse::_failure(array('reason' => 'server error'));

// Result: {"status": "failure", "reason": "server error"}
echo