A Comprehensive Guide to the Oyta PHP Common Library

ยท

The Oyta PHP Common library is a versatile and powerful toolkit designed for PHP developers. It streamlines numerous common tasks, from secure password handling to complex cryptocurrency operations. This guide provides a detailed overview of its features and practical usage.

Core Features and Capabilities

This library is built for PHP 8.0 and above, requiring the GMP extension for certain cryptocurrency-related functions. It is a self-contained, static utility package ideal for rapid application development.

Password Security

Handle user passwords with robust security practices. The library provides methods to hash a password with a unique salt and to verify a password against that stored hash.

// Hashing a password
$pass = $this->setPass('123456');
$salt = $pass['salt'];
$password = $pass['pass'];

// Verifying a password
if($this->verPass($salt,'123456') != $password)

Financial and Data Formatting

Format monetary values with comma separators for improved readability in financial applications.

$m = 123456.123456;
$mo = $this->setFotMoney($m,4);
// Output: 123,456.1234

Easily fetch real-time exchange rates for various fiat currencies and cryptocurrencies to power financial dashboards or conversion tools.

$rate = $this->getRate('cny'); // Base currency
$usdtRate = $rate['usdt']; // Get the CNY to USDT rate
$btcRate = $rate['btc']; // Get the CNY to BTC rate

Utility Generators

Generate cryptographically secure random strings tailored to your needs, from simple numbers to complex combinations.

$this->setRound(5); // Default: lowercase letters
$this->setRound(5,'n'); // Numbers only
$this->setRound(5,'ns'); // Numbers and lowercase letters

Implement Two-Factor Authentication (2FA) using Google Authenticator standards. The library helps you set up a secret, generate a QR code for users to scan, and verify entered codes.

// Setting up a new secret
$gcode = $this->setGcode('YourApp-Username'); // Returns secret and QR code data

// Retrieving the QR code for an existing secret
$qrCode = $this->getGcode('YourApp-Username', $secret);

// Verifying a user's code
$isValid = $this->verGcode($secret, $user_input_code); // Returns true or false

Cryptocurrency Operations

The library offers extensive support for major blockchain networks, enabling address generation, transaction history lookup, and even creating and broadcasting transactions.

Bitcoin (BTC) Taproot Addresses:
Generate new addresses or derive them from existing private keys. Monitor transactions for payments or other on-chain activity.

// Generate a new BTC address and private key
$btcWallet = $this->getBtcAddress();

// Derive an address from a private key
$btcAddress = $this->getBtcMultiAddress($privateKey);

// Query standard BTC transaction history
$transactions = $this->getBtcTransferRecord($address);

// Query transactions for specific tokens (Runes, BRC-20, etc.)
$tokenTransactions = $this->getBtcTokenTransferRecord($address, 'brc20', $tokenId, 50);

Ethereum (ETH) & ERC-20 Tokens:
Manage Ethereum wallets, check balances, and facilitate transfers of ETH or any standard ERC-20 token like USDT.

// Create or derive an Ethereum wallet
$ethWallet = $this->getEthAddress();
$ethAddress = $this->getEthMultiAddress($privateKey);

// Check transaction history
$ethTxs = $this->getEthTransferRecord($address);
$usdtTxs = $this->getEthTokenTransferRecord($address);

// Execute an ETH transfer
$this->EthTransfer($priv, $from, $to, '0.01');

// Execute an ERC-20 token transfer
$this->EthTokenTransfer($priv, $from, $to, '10', $tokenContractAddress);

Tron (TRX) & TRC-20 Tokens:
Access similar functionality for the Tron network, including TRX and popular TRC-20 tokens like USDT.

// Generate Tron addresses
$tronWallet = $this->getTronAddress();
$tronAddress = $this->getTronMultiAddress($privateKey);

// Query transactions
$trxTxs = $this->getTronTransferRecord($address);
$trc20Txs = $this->getTronTokenTransferRecord($address, $customTokenAddress);

// Transfer TRX or TRC-20 tokens
$this->TronTransfer($priv, $from, $to, '50');
$this->TronTokenTransfer($priv, $from, $to, '100', $tokenContractAddress);

๐Ÿ‘‰ Explore advanced blockchain integration methods

Additional Utilities

Stock Market Data:
Fetch real-time and historical data for stocks directly from Sina Finance.

// Get real-time quote for stock code '000001'
$realTimeData = $this->getSinaList('000001');

// Get historical minute-bar (K-line) data
$historyData = $this->getSinaTraderHistory('000001', 60, 200); // 60-minute intervals, last 200 periods

Email Dispatch:
Send emails seamlessly through SMTP servers, supporting SSL encryption for secure communication.

$this->SendEmail($host, $user, $pass, $ssl, $port, $fromEmail, $fromName, $toEmail, $subject, $body);

Invitation System:
Create a simple yet effective invite-code system that encodes a user ID into a short, reversible code without needing database lookups.

// Encode a user ID into a 7-character invite code
$inviteCode = $this->setInvite(9876543210); // Returns something like '9RFYGHZ'

// Decode an invite code back to the original user ID
$originalUserId = $this->getInvite('9RFYGHZ'); // Returns 9876543210

Cryptography:
Generate secure ECDSA private keys and perform AES encryption and decryption for sensitive data.

// Generate a new secp256k1 private key (used in Bitcoin/ETH)
$ecdsaPrivateKey = $this->getEcdsa();

// Encrypt and decrypt data using AES
$data = ['sensitive' => 'information'];
$encryptedData = $this->en_data($data);
$decryptedData = $this->de_data($encryptedData);

Installation and Basic Usage

Integrating the library into your project is straightforward using Composer.

composer require oyta/common

Once installed, simply extend the main class in your application code to access all its methods.

use Oyta\Common\Common;

class MyApplicationClass extends Common {
    // You now have access to all methods via $this->
}

The library adheres to PSR-2 naming conventions and PSR-4 autoloading standards, ensuring clean and interoperable code.

Frequently Asked Questions

What are the server requirements for this library?
You need to be running PHP 8.0 or a later version. For virtual currency functions, the GMP extension must be installed and enabled on your server.

How secure is the password hashing method?
The setPass and verPass methods implement strong salting and hashing techniques, providing a robust level of security for storing and verifying user credentials.

Can I use this to create a complete cryptocurrency payment gateway?
Yes, the combination of address generation and transaction listening functions provides a solid foundation for building a payment gateway for Bitcoin, Ethereum, and Tron. ๐Ÿ‘‰ Get advanced methods for payment integration

Does the stock data feature provide real-time quotes?
Yes, the getSinaList method fetches real-time data from Sina Finance, while getSinaTraderHistory retrieves historical minute-bar data for charting.

Is the email function compatible with major providers like Gmail?
Yes, as long as you provide the correct SMTP settings (host, port, SSL preference) and a valid app password or authorization code from your email provider, it will work.

What is the advantage of the invite code system?
The main advantage is that it is stateless. You can encode a user ID into a short code and decode it later without having to store the code in a database, simplifying your application logic.