PHP code example of basalam / php-sdk

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

    

basalam / php-sdk example snippets


use Basalam\BasalamClient;
use Basalam\Auth\PersonalToken;

// Personal Token (Token-based authentication)
$auth = new PersonalToken(
    token: "your-access-token",
    refreshToken: "your-refresh-token"
);

$client = new BasalamClient(auth: $auth);

function main()
{
    // Setup
    $auth = new PersonalToken(
        token: "your-access-token",
        refreshToken: "your-refresh-token"
    );
    $client = new BasalamClient(auth: $auth);

    // Get products
    $products = $client->getProducts();
    echo "Found " . count($products) . " products\n";

    // Print first few products
    foreach (array_slice($products, 0, 3) as $product) {
        echo "Product: {$product->getName()} - Price: {$product->getPrice()}\n";
    }

    return $products;
}

// Run the function
$result = main();

use Basalam\BasalamClient;
use Basalam\Auth\PersonalToken;

$auth = new PersonalToken(
    token: "your_existing_access_token",
    refreshToken: "your_existing_refresh_token"
);

$client = new BasalamClient(auth: $auth);

use Basalam\BasalamClient;
use Basalam\Auth\AuthorizationCode;
use Basalam\Auth\Scope;

// Create auth object
$auth = new AuthorizationCode(
    clientId: "your-client-id",
    clientSecret: "your-client-secret",
    redirectUri: "https://your-app.com/callback",
    scopes: [Scope::CUSTOMER_WALLET_READ, Scope::CUSTOMER_ORDER_READ]
);

// Get authorization URL
$authUrl = $auth->getAuthorizationUrl(state: "optional_state_parameter");
echo "Visit: {$authUrl}\n";

// Exchange code for tokens (after user authorization)
$tokenInfo = $auth->getToken(code: "authorization_code_from_callback");

// Create authenticated client
$client = new BasalamClient(auth: $auth);

use Basalam\BasalamClient;
use Basalam\Auth\ClientCredentials;
use Basalam\Auth\Scope;

$auth = new ClientCredentials(
    clientId: "your-client-id",
    clientSecret: "your-client-secret",
    scopes: [Scope::CUSTOMER_WALLET_READ, Scope::VENDOR_PRODUCT_WRITE]
);

$client = new BasalamClient(auth: $auth);

use Basalam\Core\Models\CreateVendorSchema;

// Create a new vendor
$vendor = $client->createVendor(
    userId: 123,
    request: new CreateVendorSchema([
        'title' => "My Store",
        'identifier' => "store123",
        'category_type' => 1,
        'city' => 1,
        'summary' => "A great store for all your needs"
    ])
);

// Get vendor details
$vendorDetails = $client->getVendor(vendorId: $vendor->getId());

use Basalam\Chat\Models\MessageRequest;

// Create a message
$message = $client->createMessage(
    request: new MessageRequest([
        'chat_id' => 123,
        'content' => "Hello, how can I help you?",
        'message_type' => "text"
    ]),
    userAgent: "MyApp/1.0",
    xClientInfo: "web"
);

// Get messages from a chat
$messages = $client->getMessages(
    chatId: 123,
    limit: 20,
    order: "DESC"
);

use Basalam\Order\Models\CreatePaymentRequestModel;

// Get active baskets
$baskets = $client->getBaskets(refresh: true);

// Create payment for invoice
$payment = $client->createInvoicePayment(
    invoiceId: 123,
    request: new CreatePaymentRequestModel([
        'payment_method' => "credit_card",
        'amount' => 10000
    ])
);

use Basalam\OrderProcessing\Models\OrderFilter;

// Get orders with filters
$orders = $client->getCustomerOrders(
    filters: new OrderFilter([
        'coupon_code' => "SAVE10",
        'cursor' => "next_cursor_123",
        'customer_ids' => "123,456,789",
        'customer_name' => "John Doe"
    ])
);

// Get specific order details
$order = $client->getCustomerOrder(orderId: 123);

use Basalam\Wallet\Models\SpendCreditRequest;

// Get user balance
$balance = $client->getBalance(userId: 123);

// Create an expense
$expense = $client->createExpense(
    userId: 123,
    request: new SpendCreditRequest([
        'reason_id' => 1,
        'reference_id' => 456,
        'amount' => 10000,
        'description' => "Payment for order #456",
        'types' => [1, 2],
        'settleable' => true
    ])
);

use Basalam\Search\Models\ProductSearchModel;

// Search for products
$results = $client->searchProducts(
    request: new ProductSearchModel([
        'query' => "laptop",
        'category_id' => 123,
        'min_price' => 100000,
        'max_price' => 500000,
        'sort_by' => "price",
        'sort_order' => "asc",
        'page' => 1,
        'per_page' => 20
    ])
);

use Basalam\Upload\Models\UserUploadFileTypeEnum;

// Upload a file
$result = $client->uploadFile(
    file: "image.jpg",
    fileType: UserUploadFileTypeEnum::PRODUCT_PHOTO,
    customUniqueName: "my-product-image",
    expireMinutes: 1440  // 24 hours
);

echo "File uploaded: {$result->getUrl()}\n";

use Basalam\Webhook\Models\CreateWebhookRequest;

// Create a new webhook
$webhook = $client->createWebhook(
    request: new CreateWebhookRequest([
        'service_id' => 1,
        'event_ids' => ["order.created", "payment.completed"],
        'request_method' => "POST",
        'url' => "https://your-app.com/webhook",
        'is_active' => true
    ])
);

// Get webhook events
$events = $client->getWebhookEvents();

// List a vendor's shipping profiles
$profiles = $client->shipping->readVendorProfiles(page: 1, perPage: 20, vendorId: 123);

// Create a profile
use Basalam\Shipping\Models\CreateProfileRequest;
$profile = $client->shipping->createProfile(new CreateProfileRequest('My Profile', 123));

// Read available carriers
$carriers = $client->shipping->readCarriers();

// Get your stories
$stories = $client->story->myStories(count: 10);

// Story discovery feed
$discovery = $client->story->discovery();

// Hashtag feed
$feed = $client->story->hashtagFeed(hashtag: 'basalam');

// List available plans
$plans = $client->apps->listPlans();

// List transactions
$transactions = $client->apps->listTransactions(page: 1, perPage: 20);

// Verify a transaction
$result = $client->apps->verifyTransaction(hashId: "abc123");
bash
composer