PHP code example of sghimire / mobile-browser

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

    

sghimire / mobile-browser example snippets


use Sandip\Browser\Native\Facades\Browser;

// Open a URL in the in-app browser (default mode)
Browser::open('https://example.com')->open();

// Hand it off to the device's external browser instead
Browser::open('https://example.com')->external()->open();

// Fully configured in-app browser
Browser::open('https://example.com')
    ->id('support-page')                // correlate this session with its events
    ->title('Support')                   // subtitle shown under the page's own title
    ->showNavigationButtons(true)        // header back button can walk page history
    ->shareButton(true)                  // "Share…" entry in the overflow menu
    ->desktopMode(false)                 // request the mobile site (default)
    ->open();

use Sandip\Browser\Native\Facades\Browser;

Browser::close();                  // close whatever in-app browser is open
Browser::close('support-page');    // close a specific session by id

use Sandip\Browser\Native\Facades\Browser;

Browser::auth(
    'https://provider.com/oauth/authorize?response_type=code&client_id=123&redirect_uri=nativephp%3A%2F%2F127.0.0.1%2Fauth%2Fcallback&state=xyz',
    'nativephp://127.0.0.1/auth/callback'
)->id('sign-in')->auth();

use Sandip\Browser\Native\Events\Browser\AuthCompleted;
use Sandip\Browser\Native\Events\Browser\Closed;
use Illuminate\Support\Facades\Event;

Event::listen(function (AuthCompleted $event) {
    $event->callbackUrl; // string — the full redirect URL the provider sent, e.g. "nativephp://127.0.0.1/auth/callback?code=...&state=..."
    $event->params;      // array<string, string> — every query AND fragment parameter already parsed out for you (code, state, access_token, error, ...)
    $event->id;           // ?string

    // Exchange the code for tokens exactly as you would in a normal web OAuth flow.
    $tokens = Http::asForm()->post('https://provider.com/oauth/token', [
        'grant_type' => 'authorization_code',
        'code' => $event->params['code'],
        'redirect_uri' => 'nativephp://127.0.0.1/auth/callback',
        'client_id' => config('services.provider.client_id'),
        'client_secret' => config('services.provider.client_secret'),
    ])->json();
});

Event::listen(function (Closed $event) {
    // Also fires here if the flow started with auth() - reason will be one of:
    // "user_cancelled", "closed_by_app", "auth_failed", "no_browser_available".
    $event->reason;
    $event->id;
});

Browser::close('sign-in');

use Sandip\Browser\Native\Events\Browser\Opened;
use Sandip\Browser\Native\Events\Browser\Closed;
use Illuminate\Support\Facades\Event;

Event::listen(function (Opened $event) {
    $event->url;    // string — the URL that was opened
    $event->mode;   // string — "webview" or "external"
    $event->id;     // ?string — matches the id you passed to ->id(), if any
});

Event::listen(function (Closed $event) {
    $event->reason; // ?string — e.g. "user_closed", "closed_by_app", "load_error", "no_browser_available"
    $event->id;     // ?string
});

use Livewire\Component;
use Sandip\Browser\Native\Attributes\OnNative;
use Sandip\Browser\Native\Events\Browser\Opened;
use Sandip\Browser\Native\Events\Browser\Closed;
use Sandip\Browser\Native\Events\Browser\AuthCompleted;
use Sandip\Browser\Native\Facades\Browser;

class SupportBrowser extends Component
{
    public bool $isOpen = false;

    public function openSupport(): void
    {
        Browser::open('https://example.com/support')->id('support-page')->open();
    }

    public function signIn(): void
    {
        Browser::auth($this->buildAuthorizeUrl(), 'nativephp://127.0.0.1/auth/callback')
            ->id('sign-in')
            ->auth();
    }

    #[OnNative(Opened::class)]
    public function onOpened(string $url, string $mode, ?string $id): void
    {
        $this->isOpen = true;
    }

    #[OnNative(Closed::class)]
    public function onClosed(?string $reason, ?string $id): void
    {
        $this->isOpen = false;
    }

    #[OnNative(AuthCompleted::class)]
    public function onAuthCompleted(string $callbackUrl, array $params, ?string $id): void
    {
        // exchange $params['code'] for tokens, log the user in, etc.
    }
}
bash
php artisan native:plugin:register