mikelbring
mikelbring

Reputation: 1492

CURL to Guzzle having Header issue

I am trying to set a Authorization header on guzzle. I have my request successfully working with the following CURL code:

function callApi($url,$accesstoken)
{

    $headr = array();
    $headr[] = 'Authorization:Bearer '.$accesstoken;

    //cURL starts
    $crl = curl_init();
    curl_setopt($crl, CURLOPT_URL, $url);
    curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($crl, CURLOPT_HTTPGET,true);
    $reply = curl_exec($crl);

    //error handling for cURL
    if ($reply === false) {
        print_r('Curl error: ' . curl_error($crl));
       return false;
    }
    curl_close($crl);
    return $reply;
}

How ever I get a 401 with this Guzzle code. I have tried to set the header a lot of different ways with no luck.

public function __construct()
    {
        $this->config = \Config::get('services.******');

        $this->client = new Client([
            'base_uri' => 'https://*******/' . $this->config['merchant_id']
        ]);
    }

    public function getInventoryItems()
    {
        $response = $this->client->get('items', [
            'headers' => [
                'Authorization' => 'Bearer' . $this->config['token']
            ]
        ]);

        return json_decode($response->getBody());
    }

Upvotes: 1

Views: 527

Answers (1)

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

'Authorization' => 'Bearer' . $this->config['token']

Should be

'Authorization' => 'Bearer ' . $this->config['token']

Upvotes: 3

Related Questions