我正在执行一个cURL请求,并且大多数情况下它都有效,但对于某些网站,它什么都没有带来,并且cURL没有错误。有人可以给我一些帮助吗?
这是我的小应用程序:http://www.convurgency.com/tools/googlebot.php
去那里并进入这个网站:http://www.beemak.com
正如你可以看到很多网站的工作,但选定的网站不...任何想法?
这是我的代码:
<?php
//Bot Curl Request
$handle = curl_init();
curl_setopt_array($handle,array(
CURLOPT_URL => $_GET['site'],
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
));
$output = curl_exec($handle);
$httpcode = curl_getinfo($handle, CURLINFO_TOTAL_TIME);
$connecttime = curl_getinfo($handle, CURLINFO_CONNECT_TIME);
$downloadtime = curl_getinfo($handle, CURLINFO_SPEED_DOWNLOAD);
$downloadsize = curl_getinfo($handle, CURLINFO_SIZE_DOWNLOAD);
if(curl_errno($handle)){
echo '<img class="errorlogo" src="http://www.convurgency.com/images/logo103.png" />';
echo '<p style="text-align:center;">There was an error finding your site, are you sure it exists?</p>';
echo '<p style="text-align:center;"><a href="http://www.convurgency.com/tools/googlebot.php">Back to GoogleBot View</a></p>';
echo 'Curl error: ' . curl_error($handle);
} else {
echo 'No Errors';
};
if (curl_error($handle)) {
print "ERROR ". curl_error($handle) ."\n<br/>";
}
curl_close($handle);
$output2 = preg_replace(
array(
// Remove invisible content
'@<head[^>]*?>.*?</head>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<applet[^>]*?.*?</applet>@siu',
'@<noframes[^>]*?.*?</noframes>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
// Add line breaks before and after blocks
'@</?((address)|(blockquote)|(center)|(del))@iu',
'@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
'@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
'@</?((table)|(th)|(td)|(caption))@iu',
'@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
'@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
'@</?((frameset)|(frame)|(iframe))@iu',
),
array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", ), $output );
echo preg_replace('/<(\w+) [^>]+>/', '<$1>', $output2);
?>
答案 0 :(得分:0)
查看$ connecttime和$ downloadtime并检查请求是否超时。检查您是否可以使用命令行curl或wget从运行脚本的服务器访问该网站。
答案 1 :(得分:0)
目标站点URL进行重定向可能是个问题。
默认情况下,当curl在发出请求后收到重定向时,不会向新URL发出请求。例如,考虑URL http://www.facebook.com。使用此URL发出请求时,服务器会将HTTP 3XX重定向发送到https://www.facebook.com/。 如果尝试以下操作,将得到一个空输出:
$ curl http://www.facebook.com
$
如果您希望cURL遵循这些重定向,则应使用-L选项。
$ curl -L http://www.facebook.com/
<full content of raw html page here>