首页 > 解决方案 > 如何捕捉“无法满足请求。” 页面/错误的 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 . '">';
                       }

标签: php

解决方案


您使用错误的变量名来检查 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;
}

推荐阅读