Login and fetching data from an external website uisng yii

hi all,

I am developing an application using YII. I have an issue in my app now.

I have an external url and I need to login ( I have username & password) and fetch details into my yii app. How can I do.Any options available in yii ?

Please help

Thanks

Anil

Hi Anil,

External URL means you are pointing at a API based login or some other ?

Thanks

It’s an API.I need to login and the url will check these credentials and will give results in xml & Json

Hi Anil,

Make use of the following interface




\yii\web\IdentityInterface



And rewrite the functions findIdentity($id), findByUsername($username), findIdentityByAccessToken($token,$type) based on your API call and make sure to decode the JSON into array formate.

May be





<?php


namespace app\models;


class User extends \yii\base\Object implements \yii\web\IdentityInterface {


    public $id;

    public $username;

    public $password;

    public $authKey;

    public $accessToken;

    private static $users = [

        '100' => [

            'id' => '100',

            'username' => 'admin',

            'password' => 'admin',

            'authKey' => 'test100key',

            'accessToken' => '100-token',

        ],

        '101' => [

            'id' => '101',

            'username' => 'demo',

            'password' => 'demo',

            'authKey' => 'test101key',

            'accessToken' => '101-token',

        ],

    ];


    /**

     * @inheritdoc

     */

    public static function findIdentity($id) {

        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; // Your API call based on id search

    }


    /**

     * @inheritdoc

     */

    public static function findIdentityByAccessToken($token, $type = null) {

        foreach (self::$users as $user) {

            if ($user['accessToken'] === $token) {

                return new static($user);

            }

        }


        return null;

    }


    /**

     * Finds user by username

     *

     * @param string $username

     * @return static|null

     */

    public static function findByUsername($username) {

        foreach (self::$users as $user) {

            if (strcasecmp($user['username'], $username) === 0) {

                return new static($user); //Your API search by username

            }

        }


        return null;

    }


    /**

     * @inheritdoc

     */

    public function getId() {

        return $this->id;

    }


    /**

     * @inheritdoc

     */

    public function getAuthKey() {

        return $this->authKey;

    }


    /**

     * @inheritdoc

     */

    public function validateAuthKey($authKey) {

        return $this->authKey === $authKey;

    }


    /**

     * Validates password

     *

     * @param string $password password to validate

     * @return bool if password provided is valid for current user

     */

    public function validatePassword($password) {

        return $this->password === $password;

    }


}




[b]

[/b]

[b]

[/b]