Ubaidullah MS
Ubaidullah MS

Reputation: 35

How to get meta tags in url using php

I wrote code like this:

$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_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
$html = $data;

//parsing begins here:
$doc = new \DOMDocument();
@$doc->loadHTML($html);
$metas = $doc->getElementsByTagName('meta');

This code is work currently but some URLs block PHP scripts to prevent scraping. How to fix this problem?

Upvotes: 2

Views: 1690

Answers (2)

user8046090
user8046090

Reputation:

You can extract all meta tags using following way :

$tags = get_meta_tags('http://www.example.com/');

// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author'];       // name
echo $tags['keywords'];     // php documentation
echo $tags['description'];  // a php manual
echo $tags['geo_position']; // 49.33;-86.59

Upvotes: 2

Arafath
Arafath

Reputation: 1090

add user_agent it will work

 curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

Upvotes: 3

Related Questions