【问题标题】:Meta description being returned in wrong language using php使用 php 以错误的语言返回元描述
【发布时间】: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 标头,没有别的。

标签: php html tags meta


【解决方案1】:

get_meta_tags 发送的 HTTP 请求不包含普通 Web 浏览器发送的传统 Accept-Language 标头以通知服务器哪种语言可能合适。

似乎某些网站(例如 Twitter)将使用地理 IP 查找来确定内容语言:

来自我在瑞典的本地计算机

Koppla direkt upp dig mot det som är viktigast för dig。 Följ dina vänner, 专家, favouritkändisar, och nyheter。

来自我在英国伦敦的 VPS

立即连接到对您来说最重要的事情。关注您的朋友、专家、喜爱的名人和突发新闻。

因此,如果您打算只查看英文元数据,您似乎需要使用Accept-language 以及可能的其他方式使您的脚本像英文本地化网络浏览器一样。

编辑:这是how to extract the meta tags by first fetching the HTML using cURL 的示例。 setting the cURL headers to include Accept-Language的详细信息。

代码示例

<?php
function file_get_contents_curl($url)
{
$ch = curl_init();

$header = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$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";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$html = file_get_contents_curl("http://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 尝试了这个并得到了相同的结果 => chillwebdesigns.co.uk/tools/4/test.php,请参阅上面的更新代码。
  • 仅仅使用cURL是不够的,需要结合第二个链接来设置Accept-Languageheader。请参阅我更新的答案中的代码示例。
  • @ChillWebDesigns 您的代码的问题是 $header 未在函数中设置。您需要在file_get_contents_curl() 中添加global $header; 才能访问它。
  • 你的男人,非常感谢你。我用 Bing 和 Twitter 尝试了这个,并且都用英语工作和返回。在您的示例中,请将twitter.com 更新为twitter.com,因为它不适用于第一个 URL。谢谢
猜你喜欢
  • 1970-01-01
  • 2022-08-09
  • 2014-07-02
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多