【问题标题】:Symfony ignore 404 status code for get requestSymfony 忽略获取请求的 404 状态码
【发布时间】:2020-07-27 10:54:20
【问题描述】:

我有一个允许用户创建愿望的应用程序。我使用每个希望的标题来发出 api 请求以取消启动以下载图片。我现在有一个问题,用户可以输入一个不从 unsplash 返回任何图像的标题。在这种情况下,我想使用占位符图像,但我的代码在收到 404 错误后停止。有没有办法忽略这个错误并继续我的循环?

 public function fetchImagesFromUnsplash() {

    $wishes = $this->repository->findAll();
    foreach ($wishes as $wish) {
            try {
                $response = $this->httpClient->request('GET', 'https://api.unsplash.com/photos/random', [
                    'query' => [
                        'query' => $wish->getDescription(),
                        'client_id' => 'oa1DsGebE8ehCV9SrvcA1mCx-2QfvnufUKgsIY5N0Mk'
                    ]
                ]);

            } catch (TransportExceptionInterface $e) {
            }

            if ($response) {
                $data = $response->getContent();
                $data = json_decode($data, true);
                $imageLink = $data['urls']['raw'];
                $rawImage = file_get_contents($imageLink);

                if ($rawImage) {
                    file_put_contents("public/images/" . sprintf('imageWish%d.jpg', $wish->getId()), $rawImage);
                    $wish->setImagePath(sprintf('public/images/imageWish%d.jpg', $wish->getId()));
                } else {
                $wish->setImagePath('placeholder.png');
                }
                $this->em->flush();
            }
        }
    }

编辑:

我试过了:

  public function fetchImagesFromUnsplash() {

    $wishes = $this->repository->findAll();
    foreach ($wishes as $wish) {
            try {
                $response = $this->httpClient->request('GET', 'https://api.unsplash.com/photos/random', [
                    'query' => [
                        'query' => $wish->getDescription(),
                        'client_id' => 'oa1DsGebE8ehCV9SrvcA1mCx-2QfvnufUKgsIY5N0Mk'
                    ]
                ]);

            } catch (NotFoundHttpException $e) {
            }

            if ($response) {
                $data = $response->getContent();
                $data = json_decode($data, true);
                $imageLink = $data['urls']['raw'];
                $rawImage = file_get_contents($imageLink);

                if ($rawImage) {
                    file_put_contents("public/images/" . sprintf('imageWish%d.jpg', $wish->getId()), $rawImage);
                    $wish->setImagePath(sprintf('public/images/imageWish%d.jpg', $wish->getId()));
                } else {
                    $wish->setImagePath('placeholder.png');
                }

            }  
        }
    $this->em->flush();

}

但在第一个 404 之后它仍然停止

【问题讨论】:

    标签: symfony http get request http-status-code-404


    【解决方案1】:

    根据documentation

    当响应的 HTTP 状态码在 300-599 范围内时 (即 3xx、4xx 或 5xx)您的代码应该能够处理它。如果你 不要那样做,getHeaders() 和 getContent() 方法会抛出一个 适当的例外

    你要检查$response->getStatusCode(),或者准备处理一个ClientException(代表4xx状态码)。

    【讨论】:

    • 你说得对,不过我用过这个:$data = $response->getContent(false);这忽略了错误,而只是给了我原来的回复,谢谢!
    • @Rugo 你应该考虑检查状态码,你会以不同的方式处理 500 或 401,对吧?
    • 你说得对,那我去看看状态码,没想到那么远。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2021-08-28
    • 2019-08-15
    • 2020-09-18
    • 2021-04-10
    • 2019-10-12
    • 2023-01-21
    相关资源
    最近更新 更多