2

I have created a REST API using the Yii2 documentation. It seems to be working fine as I can use curl like this:

curl -i "https://example.com/api/v3/user" \
    -H "Accept:application/json"  \
    -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

I would now like to be able to consume this data from another Yii2 site. I am trying to use the Yii2 REST API client. I won't post the whole code as it's basically a copy of the Facebook client in yiisoft/yii2-authclient.

Does anyone know of a guide to help me amend this to comsume my API? In the first instance, I'm struggling with what to put for $authUrl and $tokenUrl.

2
  • you should go through the documentation to have a knowledge how to build Restful Web Services with Yii2 and especially go through the section Authentication Commented Mar 27, 2018 at 22:17
  • Yup, @MuhammadOmerAslam, thanks. I've already gone through all the tutorials and have a working REST API. Any idea how to consume it? Commented Mar 28, 2018 at 10:20

2 Answers 2

5

I am not sure if you need to extend outh2 class as I believe you don't have the authentication logic completed in the first Yii2 webapp, like authenticating using first webapp url then redirect to the second webapp to extract the token from url.

It could be simpler just create a component that have those methods

class YourRestClient {
  const BASE_URL = 'https://example.com/api/v3';
  private $_token = null;

  public function authenticate($username,$password){
    $client = new Client();
    $response = $client->createRequest()
    ->setMethod('POST')
    ->setUrl(BASE_URL.'/user/login')
    ->setData(['username' => $username, 'password' => $password])
    ->send();
    if ($response->isOk) {
        $this->_token = $response->data['token'];
    }
  }

    public function logout(){
      //your logut logic
    }

    public function refreshToken(){
      //your refresh logic 
    }

    public function userList(){
      $client = new Client();
      $response = $client->createRequest()
      ->setMethod('GET')
      ->setUrl(BASE_URL.'/user/users')
      ->addHeaders([
          'content-type' => 'application/json',
          'Authorization' => 'Bearer '.$_token,
      ])
      ->send();
      if ($response->isOk) {
          return $response->getData();
      }
    }
}

for more info httpclient

Sign up to request clarification or add additional context in comments.

Comments

2

If I am not wrong what you will need for this, is to use yiisoft/yii2-httpclient Ref: https://github.com/yiisoft/yii2-httpclient Add it: php composer.phar require --prefer-dist yiisoft/yii2-httpclient

Then make the call «I would probably build a model to handle this»

use yii\httpclient\Client;

$client = new Client();
$response = $client->createRequest()
    ->setMethod('GET')
    ->setUrl('https://example.com/api/v3/user')
    ->addHeaders(['Authorization' => 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'])
    ->send();
if ($response->isOk) {
    // use your data
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.