0

Is it possible to scrape a webpage with PHP without downloading some sort of PHP library or extension?

Right now, I can grab meta tags from a website with PHP like this:

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

echo $tags['author'];       // name
echo $tags['description'];  // description

Is there a similar way to grab a info like the href from this tag, from any given website:

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

I would like to be able to do it with just PHP.

Thanks!

2 Answers2

3

Try the file_get_contents function. For example:

<?php 

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

?>

You could also use the cURL library - http://php.net/manual/en/book.curl.php

falconspy
  • 750
  • 3
  • 8
  • 22
0

Use curl for more advanced functionality. You'll be able to access headers, redirections etc. 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);      
?>
Ben Schmidt
  • 261
  • 2
  • 13