dongzhen7108 2015-07-03 20:29
浏览 33
已采纳

如何检查php中是否存在url

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 adresse 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();
        }
    }
}
  • 写回答

3条回答 默认 最新

  • dongqiya9552 2015-07-04 19:58
    关注

    I found a solution that works:

    <?php
    
    namespace AdminBundle\Validator\Constraints;
    
    use Symfony\Component\Validator\Constraint;
    use Symfony\Component\Validator\ConstraintValidator;
    
    Class ContrainteUrlExistValidator extends ConstraintValidator
    {
        public function validate($url, Constraint $constraint)
        {
            //Vérifie si l'url peut être vide
            if(empty($url)&&$constraint->peutEtreVide)
            {
                return;
            }
    
            //Pattern pour trouver les url qui commence par http:// ou https://
            $pattern='/^(https?:\/\/)/';
    
            //Valide l'url et s'assure le preg_match a trouvé un match
            if(filter_var($url, FILTER_VALIDATE_URL)&&!empty(preg_match($pattern, $url, $matches)))
            {
                //Trouve l'host
                $hostname=parse_url($url, PHP_URL_HOST);
    
                //Tente de trouver l'adresse IP de l'host
                if (gethostbyname($hostname) !== $hostname)
                {
                    //Cherche les données de l'entête
                    $headers=get_headers($url);
    
                    //Tente de trouver une erreur 404
                    if(!strpos($headers[0], '404'))
                    {
                        return;
                    }
                }
            }
    
            //Crée une erreur
            $this->context->buildViolation($constraint->message)
                        ->setParameter('%string%', $url)
                        ->addViolation();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度