0

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 :(

Martin
  • 22,212
  • 11
  • 70
  • 132
Souljacker
  • 595
  • 6
  • 23

1 Answers1

1

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"',
    ],
]);
fubar
  • 16,918
  • 4
  • 37
  • 43