简体   繁体   English

(PHP) 带有授权标头的 file_get_contents

[英](PHP) file_get_contents with a Authorization Header

How do I use file_get_contents to send a authorization header如何使用 file_get_contents 发送授权标头

I currently have file_get_contents get it with a user agent but I also would like to add a authorization header so I get a functional response instead of a 401 error我目前有 file_get_contents 使用用户代理获取它,但我也想添加一个授权标头,以便我获得功能性响应而不是 401 错误

$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15\r\n"
  )
);

$context = stream_context_create($options);
$url = file_get_contents("http://websitethatneedsaauthorizationheader.com", false, $context);

I basically need to copy the authorization header from here:我基本上需要从这里复制授权标头: 授权头

and then use that on this website and not get a 401 error然后在这个网站上使用它,不会出现 401 错误来自没有标题的网站的 401 响应

this is what it is supposed to look like with the authorization header这就是授权标头的样子来自带有标题的网站的工作响应

After the \\n of the User-Agent header, add the autorization header, like this:在 User-Agent 标头的 \\n 之后,添加 autorization 标头,如下所示:

$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: SomeUser\r\nAuthorization: SomeKey"
  )
);

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

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