php bearer token,php-使用CURL设置Bearer令牌的正确方法

php-使用CURL设置Bearer令牌的正确方法

我从API端点获取了承载令牌,并设置了以下内容:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

接下来,我想使用CURL访问安全端点,但是不确定如何或在何处设置Bearer令牌。

我已经尝试过了,但是没有用:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS,$post);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$result = curl_exec($ch);

curl_close($ch);

return json_decode($result);

编辑:

根据文档,我应该这样使用承载令牌:[https://apigility.org/documentation/auth/authentication-oauth2]

GET /oauth/resource HTTP/1.1

Accept: application/json

Authorization: Bearer 907c762e069589c2cd2a229cdae7b8778caa9f07

5个解决方案

76 votes

更换:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

与:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274";

使其成为有效且有效的Authorization标头。

Hans Z. answered 2020-01-17T04:57:24Z

17 votes

这是一个cURL函数,可以发送或检索数据。 它应与支持OAuth的任何PHP应用程序一起使用:

function jwt_request($token, $post) {

header('Content-Type: application/json'); // Specify the type of data

$ch = curl_init('https://APPURL.com/api/json.php'); // Initialise cURL

$post = json_encode($post); // Encode the data array into a JSON string

$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, 1); // Specify the request method as POST

curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Set the posted fields

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // This will follow any redirects

$result = curl_exec($ch); // Execute the cURL statement

curl_close($ch); // Close the cURL connection

return json_decode($result); // Return the received data

}

在单向或双向请求中使用它:

$token = "080042cad6356ad5dc0a720c18b53b8e53d4c274"; // Get your token from a cookie or database

$post = array('some_trigger'=>'...','some_values'=>'...'); // Array of data with a trigger

$request = jwt_request($token,$post); // Send or retrieve data

SergeDirect answered 2020-01-17T04:57:48Z

8 votes

这应该工作

$token = "YOUR_BEARER_AUTH_TOKEN";

//setup the request, you can also use CURLOPT_URL

$ch = curl_init('API_URL');

// Returns the data/output as a string instead of raw data

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Set your auth headers

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

'Content-Type: application/json',

'Authorization: Bearer ' . $token

));

// get stringified data/output. See CURLOPT_RETURNTRANSFER

$data = curl_exec($ch);

// get info about the request

$info = curl_getinfo($ch);

// close curl resource to free up system resources

curl_close($ch);

Sudirman Hung answered 2020-01-17T04:58:08Z

0 votes

如果您使用的是专用令牌(例如Gitlab API),则应替换:

$authorization = "PRIVATE-TOKEN 080042cad6356ad5dc0a720c18b53b8e53d4c274";

与:

$authorization = "PRIVATE-TOKEN 080042cad6356ad5dc0a720c18b53b8e53d4c274";

GuGuss answered 2020-01-17T04:58:41Z

0 votes

食尸鬼的例子:

use GuzzleHttp\Client;

use GuzzleHttp\RequestOptions;

$token = 'your_token';

$httpClient = new Client();

$response = $httpClient->get(

'https://httpbin.org/bearer',

[

RequestOptions::HEADERS => [

'Accept' => 'application/json',

'Authorization' => 'Bearer ' . $token,

]

]

);

print_r($response->getBody()->getContents());

见[https://github.com/andriichuk/php-curl-cookbook#bearer-auth]

Serge Andriichuk answered 2020-01-17T04:59:05Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值