Yii Framework Forum: Basic authenticate with yii2-httpclient - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Basic authenticate with yii2-httpclient

#1 User is offline   mmmw 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 03-November 17

Posted 03 November 2017 - 08:05 AM

Is there better(more elegant) way to Basic authenticate with yii2-httpclient, than manually adding "Authorization" header?

$response = $client->createRequest()
            ->setFormat(Client::FORMAT_JSON)
            ->setMethod('post')
            ->setUrl('zzz.adm/api-dbs')
            ->addHeaders(['Authorization' => 'Basic ' . base64_encode($user . ":". $password)])
            ->setData([
                "actual_users" => "33",
                "actual_devices" => "222",
            ])
            ->send();


Does sth like below exists?
->basicAuth($user, $password)
0

#2 User is offline   genyded 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 11-July 17

Posted 04 November 2017 - 10:30 PM

Not really and that's not a Yii thing, that's an HTTP thing. Any header based auth methods (Basic, JWT, oAuth...) require that the header be set on each request. You (or Yii) could wrap that in a function or something with a prettier name, but that would still have literally set the header underneath anyway and adding the indirect code path on every request would just slow things down a bit.

Also, I am new so I cannot post links yet (silly forum rule), but a web search on 'Yii2 Basic Auth' will yield a few posts on how to use tokens for things like this (instead of passing user and password data on every request), and better integrate it into the overall process.

e.g. stackoverflow.com/questions/29567658/yii2-rest-simplify-basicauth
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users