首页 > 解决方案 > pass a variable inside bear token

问题描述

I can pass the bearer token using the following code in my php script:

$headers = array(
    'Authorization' => 'Bearer blablablabla',
    'Content-type' => 'application/json'
);

The above succeeds connecting. The problem is I have to pass the above token (blablabla) which comes from variable called $token.

I tried to pass like the below

$headers = array(
    'Authorization' => 'Bearer $token',
    'Content-type' => 'application/json'
);

But the above did not work and got denied. Therefore $token is not passed inbetween single quotes with space.

How can I achieve this?

标签: php

解决方案


You should use double quotation marks to pass a variable into a string. For example:

$headers = array(
    'Authorization' => "Bearer $token",
    'Content-type' => 'application/json'
);

Or you can use concatenation:

$headers = array(
    'Authorization' => 'Bearer ' . $token,
    'Content-type' => 'application/json'
);

推荐阅读