简体   繁体   English

使用PHP REST客户端进行令牌认证

[英]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. 我试图找出一种使用某些PHP客户端向REST API发出请求的方法。

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). 但是我找不到在我尝试的任何PHP客户端(Guzzle和Httpful)中设置此标头的方法。

Would anyone know how can I do this with ANY PHP client? 谁知道我该如何使用任何PHP客户端执行此操作? I just don't wanna code this client from scratch :( 我只是不想从头开始编写此客户端代码:(

The Guzzle docs have loads of examples if you dig into them a little. 如果您稍微研究一下Guzzle文档,则会有大量示例。 http://docs.guzzlephp.org/en/latest/quickstart.html#making-a-request http://docs.guzzlephp.org/en/latest/request-options.html#headers 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"',
    ],
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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