What is the fastest way to determine if a URL exists in PHP?

前端 未结 3 1588
借酒劲吻你
借酒劲吻你 2021-01-06 04:57

I need to create a function that returns if a URL is reachable or valid.

I am currently using something like the following to determine a valid url:

         


        
相关标签:
3条回答
  • 2021-01-06 05:28

    You can use curl as follows:

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true); // set to HEAD request
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // don't output the response
    curl_exec($ch);
    $valid = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200;
    curl_close($ch);
    
    0 讨论(0)
  • 2021-01-06 05:44

    You'll likely be limited to sending some kind of HTTP request. Then you can check HTTP status codes.

    Be sure to send only a "HEAD" request, which doesn't pull back all the content. That ought to be sufficient and lightweight enough.

    0 讨论(0)
  • 2021-01-06 05:47

    You could check http status code.

    Here is a code you could use to check that an url returns 2xx or 3xx http code to ensure the url works.

    <?php
    
    $url = "http://stackoverflow.com/questions/1122845";
    
    function urlOK($url)
    {
    
        $url_data = parse_url ($url);
        if (!$url_data) return FALSE;
    
       $errno="";
       $errstr="";
       $fp=0;
    
       $fp=fsockopen($url_data['host'],80,$errno,$errstr,30);
    
       if($fp===0) return FALSE;
       $path ='';
       if  (isset( $url_data['path'])) $path .=  $url_data['path'];
       if  (isset( $url_data['query'])) $path .=  '?' .$url_data['query'];
    
       $out="GET /$path HTTP/1.1\r\n";
       $out.="Host: {$url_data['host']}\r\n";
       $out.="Connection: Close\r\n\r\n";
    
       fwrite($fp,$out);
       $content=fgets($fp);
       $code=trim(substr($content,9,4)); //get http code
       fclose($fp);
       // if http code is 2xx or 3xx url should work
       return  ($code[0] == 2 || $code[0] == 3) ? TRUE : FALSE;
    }
    
    echo $url;
    if (urlOK($url)) echo " is a working URL";
        else echo " is a bad URL";
    ?>
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题