PHP code example of mobius-network / mobius-client-php

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

    

mobius-network / mobius-client-php example snippets


// Define STELLAR_PUBLICNET
define('STELLAR_PUBLICNET', false); // System is testnet. Change it to true for publicnet

// Define SECRET_KEY
define('SECRET_KEY', 'YOUR SECRET KEY GOES HERE');

// Define JWT Secret
define('JWT_SECRET', 'YOUR JWT SECRET GOES HERE');

if(!isset($_GET['xdr'])){
    $expire_in = 86400; // Session duration
    
    // Generates and returns challenge transaction XDR signed by application to user
    $ch = Mobius\Client\Auth\Challenge::generate_challenge(SECRET_KEY, $expire_in);
    echo $ch;
}
else if(isset($_GET['xdr']) && $_GET['public_key']){
    // Validates challenge transaction. It must be:
    // - Signed by application and requesting user.
    // - Not older than 10 seconds from now (see Mobius\Client::STRICT_INTERVAL`)
    
    $xdr = base64_decode($_GET['xdr']); // It must be base64 decoded to pass to Token class

    $public_key = $_GET['public_key'];   

    $token = new Mobius\Client\Auth\Token(SECRET_KEY, $xdr, $public_key);

    $token->validate(); // Validate Token

    // Converts issued token into JWT and sends it to user.
    $jwt_token = new Mobius\Client\Auth\JWT(JWT_SECRET);
    echo $jwt_token->encode($token); // We can also store the token string in PHP $_SESSION
}

// Define STELLAR_PUBLICNET
define('STELLAR_PUBLICNET', false); // System is testnet. Change it to true for publicnet

// Define SECRET_KEY
define('SECRET_KEY', 'YOUR SECRET KEY GOES HERE');

// Define JWT Secret
define('JWT_SECRET', 'YOUR JWT SECRET GOES HERE');

$token = $_GET['token']; // Get it from $_SESSION if you have stored there.
$jwt = new Mobius\Client\Auth\Jwt(JWT_SECRET);
$token = $jwt->decode($token);  

$price = 5; // Price to be charged to the user
$app = new Mobius\Client\App(SECRET_KEY, $token->public_key);
$app->charge($price);
echo $app->user_balance(); // return the remaining balance of user's account
die;