使用PHP从网页获取信息


Grabbing information from a webpage using PHP

是否可以在不下载某种PHP库或扩展的情况下用PHP抓取网页?

现在,我可以用PHP从网站上获取元标签,如下所示:

$tags = get_meta_tags('www.example.com/');
echo $tags['author'];       // name
echo $tags['description'];  // description

有没有类似的方法可以从任何给定的网站上从这个标签中获取类似href的信息:

<link rel="img_src" href="image.png"/>

我希望只使用PHP就可以做到这一点。

谢谢!

尝试file_get_contents函数。例如:

<?php 
$data = file_get_contents('www.example.com');
$regex = '/Search Pattern/';
preg_match($regex,$data,$match);
var_dump($match); 
echo $match[1];
?>

您也可以使用cURL库-http://php.net/manual/en/book.curl.php

使用curl可以获得更高级的功能。您将能够访问标头、重定向等PHP Curl

<?php 
    $c = curl_init();
    // set some options
    curl_setopt($c, CURLOPT_URL, "google.com"); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    $data = curl_exec($c); 

    curl_close($c);      
?>