Basic authenticate with yii2-httpclient

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)

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

Hi, I’d also need to create a basic auth function (salesforce api) by passing a static params (username, pass etc). What you sugges? going with http library or oauth library?? if oauth wchich one? openid or oauth2??