PHP code example of bazarin / bazarin-php-library

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

    

bazarin / bazarin-php-library example snippets




$query = new QueryBuilder($conn);

$results = $query->select('users', '*', ['id' => 1]);

$newId = $query->insert('users', ['name' => 'Jane Doe', 'email' => '[email protected]']);

$affectedRows = $query->update('users', ['email' => '[email protected]'], ['id' => 1]);

$deletedRows = $query->delete('users', ['id' => 1]);

try {
    $query->select('non_existing_table');
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}




$apiManager = new ApiManager(['Authorization: Bearer your_api_token'], true);

$response = $apiManager->fetchAll('https://api.example.com/items');

$newItem = $apiManager->create('https://api.example.com/items', [
    'name' => 'Item Name',
    'description' => 'Description of the item'
]);

$updateResponse = $apiManager->update('https://api.example.com/items/1', [
    'name' => 'Updated Name'
]);

$deleteResponse = $apiManager->delete('https://api.example.com/items/1');

$response = $apiManager->fetchAll('https://api.example.com/items', ['Custom-Header: value']);

$apiManager->enableDebugMode(true);

try {
    $response = $apiManager->fetchAll('https://api.example.com/items');
} catch (RuntimeException $e) {
    echo "Error: " . $e->getMessage();
}

$apiManager = new ApiManager(['Authorization: Bearer your_api_token'], true);

try {
    // Fetch items
    $items = $apiManager->fetchAll('https://api.example.com/items');
    print_r($items);

    // Create an item
    $newItem = $apiManager->create('https://api.example.com/items', ['name' => 'New Item']);
    echo "Created Item ID: " . $newItem['id'];

    // Update an item
    $updatedItem = $apiManager->update('https://api.example.com/items/1', ['name' => 'Updated Name']);
    echo "Updated Item: " . json_encode($updatedItem);

    // Delete an item
    $apiManager->delete('https://api.example.com/items/1');
    echo "Item deleted successfully.";
} catch (RuntimeException $e) {
    echo "API Error: " . $e->getMessage();
}

  $fileGetContent = new FileGetContent('https://example.com');
  $data = $fileGetContent->get_content();
  print_r($data); // Output the parsed JSON payload
  

// Include the class file
n
$fileHandler = new FileGetContent('https://your-frontend-domain.com');

// Retrieve the JSON content from the request
$data = $fileHandler->get_content();

// Process the incoming data
if ($data) {
    echo json_encode([
        'status' => 'success',
        'message' => 'Data received successfully',
        'data' => $data,
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => 'Invalid or missing JSON payload',
    ]);
}

$crypt = new Crypt("my_secret_key");  // Initializes with the encryption key

$crypt = new Crypt("my_secret_key");
$plaintext = "Hello, this is a secret message!";
$encryptedData = $crypt->encrypt($plaintext);
echo $encryptedData;  // Encrypted data in base64 format

$crypt = new Crypt("my_secret_key");
$encryptedData = "base64_encrypted_data_with_iv";  // The base64-encoded encrypted string
$decryptedData = $crypt->decrypt($encryptedData);
echo $decryptedData;  // Decrypted message

    $crypt = new Crypt("my_secret_key");
    $plaintext = "Sensitive information that needs encryption.";
    $encrypted = $crypt->encrypt($plaintext);
    echo $encrypted;
    

    $crypt = new Crypt("my_secret_key");
    $encryptedData = "base64_encrypted_string";
    $decrypted = $crypt->decrypt($encryptedData);
    echo $decrypted;  // Should display the original plaintext
    

try {
    // File to be uploaded
    $file = $_FILES['file'];

    // Destination path
    $destination = 'uploads/' . basename($file['name']);

    // Optional validation for file type and size
    $allowedTypes = ['image/jpeg', 'image/png'];
    $maxSize = 2000000; // 2MB

    // Attempt to upload the file
    $uploadedFile = FileHelper::upload($file, $destination, $allowedTypes, $maxSize);
    echo "File uploaded successfully: $uploadedFile";
} catch (\Exception $e) {
    // Handle any errors that occur during the upload
    echo "Error: " . $e->getMessage();
}

try {
    // Format a date string to the default format (Y-m-d)
    $formattedDate = DateHelper::format('2024-11-29');
    echo $formattedDate; // Output: 2024-11-29

    // Format a date string with a custom format (d/m/Y)
    $formattedDate = DateHelper::format('2024-11-29', 'd/m/Y');
    echo $formattedDate; // Output: 29/11/2024

    // Format a DateTime object with a custom format (l, F j, Y)
    $date = new \DateTime('2024-11-29');
    $formattedDate = DateHelper::format($date, 'l, F j, Y');
    echo $formattedDate; // Output: Friday, November 29, 2024
} catch (\Exception $e) {
    // Handle any errors that occur during the date formatting
    echo "Error: " . $e->getMessage();
}