How to load a webpage in php

I was wondering how can I load a webpage in php for parsing?

+9
php parsing webpage
source share
5 answers

You can use something like this

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

Since you probably want to analyze the page using the DOM , you can load the page directly:

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

when your PHP allow_url_fopen is enabled.

But in principle, you can use any function that supports HTTP stream wrappers to load the page.

+10
source share

With the curl library.

+8
source share

Just add another option because it is, although it is best to use a file. His other option, which I do not see anyone, is listed here.

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

Well, if you want it in an array of strings, while the already mentioned get_contents file will put it in a string.

Another thing you can do.

Then you can loop through each line if that suits your purpose:

 foreach($array as $line){ echo $line; // do other stuff here } 

This is sometimes useful when some APIs splash out plain text or html with a new entry on each line.

+6
source share

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; 
+3
source share

All Articles