2013-08-01 41 views
0

我不知道是否有人能夠解釋我遇到的問題。我正在構建一個查看網站標題和說明元標記的SEO工具。我所經歷過的是使用使用php返回錯誤語言的元描述

<?php 

$tags = get_meta_tags("https://twitter.com"); 
echo $tags['description']; 
?> 

我得到在德國返回的描述

「Verbinde荻sofort MIT書房Dingen,死für荻上午wichtigsten信德。Folge Freunden,專家組織,Lieblingsstars UND aktuellen 新聞報」

,而不是在英語

「立即連接到對您最重要的部分。關注您的朋友,專家,最喜歡的名人和突發新聞。」

我還發現,Bing.com我也有這個問題,有太多。我試過捲曲這也並得到了相同的結果。

這是什麼模樣,

<? 

$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"; 

?> 

我捲曲代碼捲曲響應這裏運行=>http://www.chillwebdesigns.co.uk/tools/4/test.php

有誰遇到這之前?

+0

你是什麼區域? – DevZer0

+0

我在英國,我託管的服務器是One.com。我也認爲這個,但是當在http://web-sniffer.net/上測試時,他們在他們的網站上出現了相同的問題。 –

+0

以及下面建議的一些解決方法,但我也想知道爲什麼會發生這種情況。因此,您可以確認請求源IP與奧地利或德國無關 – DevZer0

回答

4

get_meta_tags發送的HTTP請求不包含傳統的Accept-Language標頭,即正常的Web瀏覽器發送的標頭,以便通知服務器哪種語言可能是合適的。

這似乎是一些網站(如Twitter)上會使用地理IP查詢以確定內容的語言:

從我的本地計算機在瑞典

Koppla DIREKT UPP挖MOT DET索姆AR viktigastfördig。 Följdinavänner,專家,最喜歡的人,最喜歡的人。

從我在倫敦的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"; 

?> 
+0

感謝您的回答,我用curl試過這個,得到了相同的結果=> http://www.chillwebdesigns.co.uk/tools/4/test.php,請參閱上面的更新代碼。 –

+0

只需使用cURL是不夠的,您需要將它與第二個鏈接結合起來,以瞭解如何設置「Accept-Language」標題。查看我更新的答案中的代碼示例。 –

+0

@ChillWebDesigns您的代碼存在的問題是$ header沒有在函數中設置。您需要在'file_get_contents_curl()'內部添加'global $ header;'以訪問它。 –