0

I want to check if the status of the page is not 200, then add it to the array along with the status and send it to the mail at the end of the script

I can't finish the code, tell me how to implement it. My code stops working on the line $this->curl->get($url);

<?php

namespace Modules\Test\Cron;

class Test
{
    protected $curl;

    private $logger;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\HTTP\Client\Curl $curl
    ) {
        $this->logger = $logger;
        $this->curl = $curl;
    }

    public function execute(): void
    {
        $urls = [
            'http://example.com/one',
            'http://example.com/two',
            'http://example.com/three'
        ];

        $problemUrls = [];

        $this->curl->setCredentials('basicLogin', 'basicPass');

        foreach ($urls as $url) {
            try {
                $this->curl->get($url);
                $response = $this->curl->getHeaders();
                if ($response === 404) {
                    array_push($problemUrls, $url);
                }

                if (!empty($problemUrls)) {
                    mail('test@gmail.com', 'test@gmail.com', var_export($problemUrls, true));
                }
            }
            catch (\Exception $e) {
                $this->logger->critical('Error Curl', ['exception' => $e]);
            }
        }
    }
}
2
  • which are your errors?
    – Diana
    May 20, 2021 at 8:09
  • I don't see a problem with the line: $this->curl->get($url); but the next line should probably be: $response = $this->curl->getStatus();
    – jiheison
    May 20, 2021 at 19:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.