I'm working on a Symfony 2 project and I'm making a custom constraint to check if an url exist. I checked around and found this:
How can I check if a URL exists via PHP?
The problem is if I try a totally random address like www.flskkhfkhsdf.com
, it gives me a warning and it stop my code. Is there an other way to do that?
The warning:
Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: No such host is known.
Here is my code:
<?php
namespace AdminBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
Class ContrainteUrlExistValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$file_headers = get_headers($value);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
}
}
}