How can I check if a URL exists via PHP?

前端 未结 22 1196
天涯浪人
天涯浪人 2020-11-22 04:13

How do I check if a URL exists (not 404) in PHP?

22条回答
  •  伪装坚强ぢ
    2020-11-22 05:15

    $url = 'http://google.com';
    $not_url = 'stp://google.com';
    
    if (@file_get_contents($url)): echo "Found '$url'!";
    else: echo "Can't find '$url'.";
    endif;
    if (@file_get_contents($not_url)): echo "Found '$not_url!";
    else: echo "Can't find '$not_url'.";
    endif;
    
    // Found 'http://google.com'!Can't find 'stp://google.com'.
    

提交回复
热议问题