Search code examples
phpcurldomdocumentmeta-tags

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?


Solution

  • 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');