【发布时间】:2013-08-01 09:09:20
【问题描述】:
我想知道是否有人可以对我遇到的问题有所了解。我正在构建一个 SEO 工具,用于查看网站标题和描述元标记。我所经历的是使用
<?php
$tags = get_meta_tags("https://twitter.com");
echo $tags['description'];
?>
我收到了德语版本的描述
"Verbinde Dich sofort mit den Dingen, die für Dich am wichtigsten sind. Folge Freunden, Experten, Lieblingsstars und aktuellen Nachrichten"
不是英语
“立即连接到对您来说最重要的事物。关注您的朋友、专家、喜爱的名人和突发新闻。”
我还发现 Bing.com 我也有这个问题。我也用 Curl 尝试过,得到了相同的结果。
这就是我的 curl 代码的样子,
<?
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl("https://twitter.com");
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
if($meta->getAttribute('language') == 'language');
$language = $meta->getAttribute('language');
}
echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";
?>
curl 响应在这里运行 => http://www.chillwebdesigns.co.uk/tools/4/test.php
以前有人遇到过这种情况吗?
【问题讨论】:
-
我在英国,我托管的服务器使用 One.com。我也这么认为,但是在web-sniffer.net 上进行测试时,他们在他们的网站上遇到了同样的问题。
-
好吧,您在下面建议了一些解决方法,但我也想知道为什么会发生这种情况。因此,您可以确认请求发起的 IP 与奥地利或德国无关
-
你能在执行脚本的同时运行wireshark或tcpdump看看它发出的请求头是什么样的吗?
-
我会尝试运行wireshark并报告
-
它只发送
Host标头,没有别的。