简体   繁体   中英

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

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"',
    ],
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM