<?php
$path = 'http://www.baidu.com/';
if(head...==200){
    echo "该页面存在!!!";
    exit();
}
?>这代码得如何写?

解决方案 »

  1.   


        function checkUrl($url, $curl=false)
        {
            $status = '200';
            if ($curl)
            {
                $ch=curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_NOSIGNAL, TRUE);
                curl_setopt($ch, CURLOPT_NOBODY, true);
                curl_setopt($ch, CURLOPT_REFERER, $url);
                //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.file');
                $result = curl_exec($ch);
                $found = false;
                if ($result !== false)
                {
                    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                    if ($statusCode == 200) $found = true;
                }
                curl_close($ch);            return $found;         } else {
                $tmpArr = get_headers($url,1);
                if(stripos($tmpArr[0],$status)) return true;
            }
            return false;
        }
      

  2.   

    由$url = 'http://www.baidu.com/';
    $h = get_headers($url, true);
    print_r($h);Array
    (
        [0] => HTTP/1.1 200 OK
        [Date] => Sat, 05 Jan 2013 10:40:59 GMT
        [Server] => BWS/1.0
        [Content-Length] => 9777
        [Content-Type] => text/html;charset=gbk
        [Cache-Control] => private
        [Expires] => Sat, 05 Jan 2013 10:40:59 GMT
        [Set-Cookie] => BAIDUID=17B9BC88E615639547C6CB06AEB14F2A:FG=1; expires=Sat, 05-Jan-43 10:40:59 GMT; path=/; domain=.baidu.com
        [P3P] => CP=" OTI DSP COR IVA OUR IND COM "
        [Connection] => Close
    )
    可知$url = 'http://www.baidu.com/';
    $h = get_headers($url, true);
    if(strpos($h[0], '200')) echo 'yes';