После долгих усилий я решил часть проблемы.
Я решил проблему с Yahoo, так что я могу получить ее информацию.
Но для Google URL не работает.
Google не имеет описания или og: описание в своем источнике, когда я получаю его по серверу.
Результат кода для Yahoo:
{
"title": "Yahoo",
"description": "News, email and search are just the beginning. Discover more every day. Find your yodel.",
"og:title": "Yahoo",
"og:type": "website",
"og:url": "http://www.yahoo.com",
"og:description": "News, email and search are just the beginning. Discover more
every day. Find your yodel.",
"og:image": "https://s.yimg.com/dh/ap/default/130909/y_200_a.png",
"og:site_name": "Yahoo"
}
Но результат кода дляGoogle является:
{
"title": "Google"
}
Пожалуйста, помогите мне о Google ....
Новый исходный код:
//In routes/api.php
Route::get('/links/helper/meta-tag-extractor', function(Request $request){
$url = $request->get('url');
$result = [];
function file_get_contents_curl($url)
{
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
// echo $html;
$nodes = $doc->getElementsByTagName('title');
if ($nodes->count()) {
$result['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') {
$result['description'] = $meta->getAttribute('content');
}
if(substr( $meta->getAttribute('property'), 0, 3 ) === 'og:') {
$result[$meta->getAttribute('property')] = $meta->getAttribute('content');
}
}
return $result;
});