我正在使用此代码获取输入的 url 的内容:-
class MetaTagParser
{
public $metadata;
private $html;
private $url;
public function __construct($url)
{
$this->url=$url;
$this->html= $this->file_get_contents_curl();
$this->set_title();
$this->set_meta_properties();
}
public function file_get_contents_curl()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function set_title()
{
$doc = new DOMDocument();
@$doc->loadHTML($this->html);
$nodes = $doc->getElementsByTagName('title');
$this->metadata['title'] = $nodes->item(0)->nodeValue;
}
这个类适用于某些页面,但适用于像这样的一些 url - http://www.dnaindia.com/india/report_in-a-first-upa-govt-tweets-the-press_1745346 当我尝试获取数据时我得到了这个错误:-“警告:get_meta_tags(http://www.dnaindia.com/india/report_in-a-first-upa-govt-tweets-the-press_1745346):未能打开流:HTTP请求失败!HTTP/1.1 403禁止在 C:\xampp\htdocs\prac\index.php 第 52 行"
它不起作用,任何想法为什么会这样?