Search code examples
symfonyhttpgetrequesthttp-status-code-404

Symfony ignore 404 status code for get request


I have an application which allows users to create wishes. I use the title of each wish to make an api request to unsplash to download a picture. I now have the problem, that a user can enter a title which doesnt return any images from unsplash. In this case I'd like to use a placeholder image but my code stops after getting an 404 error. Is there a way to ignore this error and just continue my loop?

 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();
            }
        }
    }

EDIT:

I tried this:

  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();

}

but it still stops after the first 404


Solution

  • As per the documentation:

    When the HTTP status code of the response is in the 300-599 range (i.e. 3xx, 4xx or 5xx) your code is expected to handle it. If you don't do that, the getHeaders() and getContent() methods throw an appropriate exception

    You have to check the $response->getStatusCode(), or prepare to handle a ClientException (representing 4xx status codes).