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