php - 如何捕捉“无法满足请求。” 页面/错误的 URL
问题描述
我正在尝试检测 php 中的错误 403。当我从 URL 加载内容并且出现错误 403 时,显示其他内容。使用 404 可以,但是使用 403 则不起作用。我有 2 个 URL 链接:1)404:https ://images-na.ssl-images-amazon.com/images/M/MV5BMTUwODE3MDE0MV5BMl5BanBnXkFtZTgwNTk1MjI4MzE@._V1_SX300.jpg ;2)403:http: //ia.media-imdb.com/images/M/MV5BMTg3Njc2ODEyN15BMl5BanBnXkFtZTcwNTAwMzc3NA@@._V1_SX300.jpg
我尝试了很多,也使用 http_response_code(403) 并没有成功...谢谢您的帮助...
我的代码如下:
<div class="poster">
<?php
$posterAll = $movie->posterUrl;
$file_headers=@get_headers($posterAll);
if(!$file_headers || $file_headers[0]=='HTTP/1.1 404 Not Found' || $file_headers[0]=='Not Found' || (strpos($headers[0], '403'))){
$exists=false;
}
else{
$exists=true;
}
echo $exists;
if ($exists) {
$imageData = base64_encode(file_get_contents($posterAll));
echo '<img height="300" width="300" src="data:image/jpeg;base64,' . $imageData . '">';
}
解决方案
您使用错误的变量名来检查 403。更改(strpos($headers[0], '403')
为(strpos($file_headers[0], '403')
if(!$file_headers || $file_headers[0]=='HTTP/1.1 404 Not Found' || $file_headers[0]=='Not Found' || (strpos($file_headers[0], '403'))){
$exists=false;
}
推荐阅读
- mysql - 如何将约束添加到 MYSQL 中的列仅以保存已存在于同一表的另一列中的值?
- computer-vision - 从图像匹配路线图
- ruby-on-rails - Rails 模型只接受 0 到 1 之间的浮点数
- java - 用于员工经理搜索的二叉搜索树
- c++ - 是否可以在编译时初始化数组,以便在运行时不需要时间?
- html - 如何在不使用容器元素的情况下实现这种响应式设计?
- excel - 随着时间的推移,将单元格导出为 Excel
- sql - 在 C# 和 EF Core 中播种多对多数据库
- azure - 在 Azure 数据工厂中创建触发器期间使用参数定位文件
- android - 如何在 Kotlin 中显示来自 Firebase 实时数据库的用户名?