Hugo
Hugo

Reputation: 139

How to download a webpage in php

I was wondering how I could download a webpage in php for parsing?

Upvotes: 11

Views: 32120

Answers (5)

RetroNoodle
RetroNoodle

Reputation: 95

Just to add another option because it is there, while not the best is just to use file. Its another option that I dont see anyone has listed here.

$array = file("http://www.stackoverflow.com");

Its nice if you want it in an array of lines, whereas the already mentioned file_get_contents will put it in a string.

Just another thing you can do.

Then you can loop thru each line if this matches your goal to do so:

foreach($array as $line){

    echo $line;
    // do other stuff here

}

This comes in handy sometimes when certain APIs spit out plain text or html with a new entry on each line.

Upvotes: 6

User123
User123

Reputation: 556

You can use this code

$url = 'your url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
// you can do something with $data like explode(); or a preg match regex to get the exact information you need
//$data = strip_tags($data);
echo $data;

Upvotes: 5

Gordon
Gordon

Reputation: 316969

Since you will likely want to parse the page with DOM, you can load the page directly with:

$dom = new DOMDocument;
$dom->load('http://www.example.com');

when your PHP has allow_url_fopen enabled.

But basically, any function that supports HTTP stream wrappers can be used to download a page.

Upvotes: 12

DimitrisD
DimitrisD

Reputation: 424

You can use something like this

$homepage = file_get_contents('http://www.example.com/');
echo $homepage;

Upvotes: 17

Quentin
Quentin

Reputation: 943571

With the curl library.

Upvotes: 8

Related Questions