How to do HTTP basic auth with Laravel and Guzzle

Here is a snippet that allows you to do basic auth:

<span class="pln">$client </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Client</span><span class="pun">();</span><span class="pln">

$response </span><span class="pun">=</span><span class="pln"> $client</span><span class="pun">-></span><span class="pln">request</span><span class="pun">(</span>
    <span class="str">'POST'</span><span class="pun">,</span> <span class="com">/*instead of POST, you can use GET, PUT, DELETE, etc*/</span><span class="pln">
    $url</span><span class="pun">,</span>
    <span class="pun">[</span>
      <span class="str">'auth'</span> <span class="pun">=></span> <span class="pun">[</span><span class="str">'username'</span><span class="pun">,</span> <span class="str">'password'</span><span class="pun">]</span> <span class="com">/*if you don't need to use a password, just leave it null*/</span>
    <span class="pun">]</span> 
<span class="pun">);</span><span class="pln">

echo $response</span><span class="pun">-></span><span class="pln">getBody</span><span class="pun">();</span>

Reference:
https://stackoverflow.com/questions/30970736/how-do-i-do-http-basic-authentication-using-guzzle/30970856

Share this article

Leave a Reply

Your email address will not be published.

Scroll to Top