- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直收到这个错误
Warning: file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
代码
$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);
我在我的 api 中使用的中间件通过 url 参数或 header 接受授权;
如果我在参数中使用 token ,它可以工作但不能通过服务器上的 header 发送它,导致在本地主机上它工作,
请求是通过不记名 token 授权的,我没有任何用户名和密码来授权请求,正如我在下面演示的那样,我只在我的 url 参数中使用 token 。
知道为什么吗?在不更改所有 curl 请求的情况下我能做什么
$result = file_get_contents(BASE_URL . 'api/v1/users?authorization='.$token, false);
$response = json_decode($result, true);
最佳答案
您可以将 header 添加到 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);
关于php - file_get_contents 不适用于承载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409522/
我正在使用 file_get_contents() 加载一个文本文件以放入文本区域,出于某种原因,每次加载它时,原始文本前后都会插入空格。我知道在保存过程中没有插入空格,因为我从我的 FTP 客户端检
我正在尝试使用 fsockopen 上传超过 2gb 的大文件。但是 file_get_content 出现以下错误,我无法将大文件存储在内存中。我需要分块发送数据,但不知道如何执行此操作。请问有人可
我一直收到这个错误 Warning: file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 401 Unauth
我正在尝试从 php 发送电子邮件我有一个包含所有值的 php 文件和其他 php 模板文件。 (两个文件在同一台服务器上) 例如,我正在使用 file_get_contents 获取 php 模板文
我正处于构建 PHP 应用程序的早期阶段,其中一部分涉及使用 file_get_contents()从远程服务器获取大文件并将它们传输给用户。例如,要获取的目标文件是 200 mB。 如果下载到服务器
我正在为我的博客的示例代码文件夹制作一个基本的 PHP 源代码查看器。 我这里的内容是否足以让它永远不允许查看此脚本所在目录之外的文件或此脚本目录的子目录?我猜还有比 startsWith 更好的解
我正在尝试使用 file_get_contents() 从页面获取 html。 以下效果很好:file_get_contents('http://www.mypage.com?Title=Title'
file_get_contents 是否维护换行符?我认为它做到了,但我已经尝试过: if($conn){ $tsql = file_get_contents('scripts/CreateT
我有一个具有以下架构的网站: End user ---> Server A (PHP) ---> Server B (ASP.NET & Database)
在 if 子句中用作测试条件时,如何防止 file_get_contents 创建空文件? 无论如何都会创建一个空文件,这会导致在不同方法中对 getimagesize() 的后续调用失败。 问题是,
我目前正在本地机器上测试我的代码,我希望它能够读取和写入我拥有的文本文件,所以我有以下代码: Warning: file_get_contents(~/Desktop/insta_user.txt):
在某人的服务器中,出于安全原因,file_get_contents 被禁用。我需要检索 xml 数据。那么,最好的做法是: 验证服务器是否支持file_get_contents? file_get_c
This question already has answers here: Warning: file_get_contents(): https:// wrapper is disabled i
在载入x秒钟后,是否有任何方法可以获取网页响应? 例如,我想创建一个获取youtube视频评论数量的api,但是正如您所知,当您打开youtube视频链接时,它会像加载评论一样2秒钟,因此,如果您知道
我正在尝试像这样从youtube读取视频信息: $vid='WwVZBfMlNPA'; $vurl='http://youtube.com/get_video_info?video_id='.$vid
更新 我解决了问题并发布了答案。但是,我的解决方案并不是 100% 理想。我宁愿只使用 clearstatcache(true, $target) 或 clearstatcache(true, $li
目前,我正在使用 file_get_contents() 将 GET 数据提交到网站数组,但在执行页面时出现此错误: fatal error :超出最大执行时间 30 秒 我真正希望脚本做的就是开始加
问题是当我使用file_get_contents从该网站获取源代码(HTML)时,我收到的结果不是纯html代码。 我使用的代码: $source = file_get_contents("http:
这是我的全部代码: Playlist to Scrape: "; $fullUrl = array(); foreach($output[1] as $ur
我是 PHP 新手,所以请多多关照:) 有时 file_get_contents 会完成其工作,有时则不会。我在网页上构建了一个简单的 URL 检查(如果存在)。但问题是,即使 URL 确实存在(手动
我是一名优秀的程序员,十分优秀!