Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Content of Remote HTML page

Tags:

html

php

I want to get remote html contents that on "li" that with a spacific Class name and Children of em using divs.

My remote content is Like this

<ul>

<li class="user">

<div class="name">My Name 1</div>

<div class="rep">20</div>

</li>

<li class="user">

<div class="name">My Name 2</div>

<div class="rep">23</div>

</li>

<li class="user">

<div class="name">My Name 3</div>

<div class="rep">40</div>

</li>

</ul>

After get their data it must be like this.

[My Name 1,20]

[My Name 2,23]

[My Name 3,40]

Thanks.

Sorry for the My Poor English

Note : Have more content than this on remote page.

like image 322
lilruchira Avatar asked Jul 31 '12 05:07

lilruchira


1 Answers

Use CURL to read the remote URL to fetch the HTML.

$url = "http://www.example.com";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);

Then use PHP's DOM object model to parse the HTML.

For example to fetch all <h1> tags from the source,

$DOM = new DOMDocument;
$DOM->loadHTML( $output);

//get all H1
$items = $DOM->getElementsByTagName('h1');

//display all H1 text
 for ($i = 0; $i < $items->length; $i++)
        echo $items->item($i)->nodeValue . "<br/>";
like image 149
verisimilitude Avatar answered Oct 21 '22 09:10

verisimilitude