首先确保URL实际上是有效的(字符串,不是空,语法不错),这可以快速检查服务器端。例如,首先执行此操作可以节省大量时间:
if(!$url || !is_string($url) || ! preg_match('/^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$/i', $url)){ return false;}确保仅获取标题,而不获取正文内容:
@curl_setopt($ch, CURLOPT_HEADER , true); // we want headers@curl_setopt($ch, CURLOPT_NOBODY , true); // we don't need body
整体而言:
$url = 'http://www.example.com';$ch = curl_init($url);curl_setopt($ch, CURLOPT_HEADER, true); // we want headerscurl_setopt($ch, CURLOPT_NOBODY, true); // we don't need bodycurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_TIMEOUT,10);$output = curl_exec($ch);$httppre = curl_getinfo($ch, CURLINFO_HTTP_CODE);curl_close($ch);echo 'HTTP pre: ' . $httppre;



