Souljacker
Souljacker

Reputation: 595

Token authentication with PHP REST client

I'm trying to figure out a way to make a request to a REST api using some PHP client.

Authorization: Token token="CREDENTIALS"

I can successfully curl it by using

$ curl -H 'Authorization: Token token="CREDENTIALS" https://uriexample.com

But I can't figure out a way to set this header in any PHP client I tried (Guzzle and Httpful).

Would anyone know how can I do this with ANY PHP client? I just don't wanna code this client from scratch :(

Upvotes: 0

Views: 1625

Answers (1)

fubar
fubar

Reputation: 17398

The Guzzle docs have loads of examples if you dig into them a little. http://docs.guzzlephp.org/en/latest/quickstart.html#making-a-request http://docs.guzzlephp.org/en/latest/request-options.html#headers

<?php

// Create HTTP client with headers for all requests
$client = new GuzzleHttp\Client([
    'base_uri' => 'https://uriexample.com',
    'headers' => [
        'Authorization' => 'Token token="CREDENTIALS"',
    ],
]);

// Dispatch GET request
$client->request('GET', '/');


// OR


// Create HTTP client
$client = new GuzzleHttp\Client([
    'base_uri' => 'https://uriexample.com',
]);

// Dispatch GET request with specific headers
$client->request('GET', '/', [
    'headers' => [
        'Authorization' => 'Token token="CREDENTIALS"',
    ],
]);

Upvotes: 1

Related Questions