ejlin/line-login-sdk-php

SDK of the LINE Login API for PHP

0.1.2 2022-02-27 15:09 UTC

This package is auto-updated.

Last update: 2024-09-27 20:31:44 UTC


README

SDK of the LINE Login API for PHP

Documentation

See the official API documentation for more information.

Installation

Use the package manager composer to install package.

composer require ejlin/line-login-sdk-php

Usage

###Pure PHP version:

$httpClient = new \EJLin\LINELogin\GuzzleHTTPClient();

// Channel Basic Information: https://developers.line.biz/console/channel/<channel id>/basics
$LINELogin = new \EJLin\LINELogin($httpClient, ['clientId' => '<channel id>','clientSecret' => '<channel secret>']);

// Step 3. After login, the LINE will redirect to the URL that you requested with state and code
if(isset($_GET['code']) && isset($_GET['state']))
{
    // TODO: Check the state code same as what you requested
    
    // Request access token from the LINE platform
    $token = $LINELogin->requestToken(
        'https://yourdomain.com', // The url must be the same as requested
        $_GET['code'] // Each code only can request a token once
    );
    
    // Get user profile from the LINE platform
    $userProfile = $LINELogin->getUserProfile($token);
    
    printf("Hello %s !",$userProfile->getDisplayName());
    
    exit;
}

// A unique alphanumeric string used to prevent cross-site request forgery
$state = \EJLin\LINELogin\Helper::randomString(40);

// Step 1. Make authorize url
$authorizeUrl = $LINELogin->makeAuthorizeUrl(
    'https://yourdomain.com', // Callback URL: https://developers.line.biz/console/channel/<channel id>/line-login
    'profile openid email', // Permissions requested from the user: https://developers.line.biz/en/docs/line-login/integrate-line-login/#scopes
    $state
);

// Step 2. Redirect to authorize url
header("Location: $authorizeUrl");
exit;

###Laravel support:

After installed, add LINE_LOGIN_CHANNEL_ID and LINE_LOGIN_CHANNEL_SECRET to .env

LINE_LOGIN_CHANNEL_ID=<channel id>
LINE_LOGIN_CHANNEL_SECRET=<channel secret>

then you can use LINELogin and LINELoginHelper facades like following.

// Step 3.
if(request()->has('code') && request()->has('state'))
{
    $token = \EJLin\Laravel\Facades\LINELogin::requestToken(
            url()->current(),
            request()->input('code'),
        );

    $userProfile = \EJLin\Laravel\Facades\LINELogin::getUserProfile($token);

    return "Hello {$userProfile->getDisplayName()} !";
}

$state = \EJLin\Laravel\Facades\LINELoginHelper::randomString(40);

// Step 1.
$authorizeUrl = \EJLin\Laravel\Facades\LINELogin::makeAuthorizeUrl(
    url()->current(),
    'profile openid email',
    $state
);

// Step 2.
return redirect()->away($authorizeUrl);

Reference

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT