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

前端 未结 3 1595
借酒劲吻你
借酒劲吻你 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);
    

提交回复
热议问题