mir*_*atJ 3 php middleware slim
我不断收到此错误
警告:file_get_contents 无法打开流:HTTP 请求失败!HTTP/1.1 401 未经授权
代码
$url = BASE_URL . 'api/v1/users';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$token
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
Run Code Online (Sandbox Code Playgroud)
我与 api 一起使用的中间件通过 url 参数或标头接受授权;
如果我在参数中使用令牌,它可以工作,但无法通过服务器上的标头发送它,导致在本地主机上工作,
该请求是通过不记名令牌授权的,我没有任何用户名和密码来授权该请求,如下所示,我仅在 url 参数中使用令牌。
知道为什么吗?在不更改所有卷曲请求的情况下我能做什么
$result = file_get_contents(BASE_URL . 'api/v1/users?authorization='.$token, false);
$response = json_decode($result, true);
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以将标头添加到 file_get_contents,它需要一个名为 context 的参数,可用于此目的:
$url = BASE_URL . 'api/v1/users';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$token
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11826 次 |
| 最近记录: |