2

I want the HTML code from the URL.

Actually I want following things from the data at one URL.

1. blog titile
2. blog image
3. blod posted date
4. blog description or actual blog text

I tried below code but no success.

<?php
  $c = curl_init('http://54.174.50.242/blog/');
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt(... other options you want...)

    $html = curl_exec($c);

    if (curl_error($c))
        die(curl_error($c));

    // Get the status code
    $status = curl_getinfo($c, CURLINFO_HTTP_CODE);

    curl_close($c);

    echo "Status :".$status; die;
?>

Please help me out to get the necessary data from the URL(http://54.174.50.242/blog/).

Thanks in advance.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    possible duplicate of [Get HTML content from another site](http://stackoverflow.com/questions/11438864/get-html-content-from-another-site) – Styphon Aug 25 '15 at 09:30

2 Answers2

1

You are halfway there. You curl request is working and $html variable is containing blog page source code. Now you need to extract data, that you need, from html string. One way to do it is by using DOMDocument class.

Here is something you could start with:

$c = curl_init('http://54.174.50.242/blog/');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($c);

$dom = new DOMDocument;

// disable errors on invalid html
libxml_use_internal_errors(true);

$dom->loadHTML($html);

$list = $dom->getElementsByTagName('title');
$title = $list->length ? $list->item(0)->textContent : '';

// and so on ...

You can also simpllify that by using method loadHTMLFile on DOMDocument class, that way you don't have to worry about all curl code boilerplate:

$dom = new DOMDocument;

// disable errors on invalid html
libxml_use_internal_errors(true);

$dom->loadHTMLFile('http://54.174.50.242/blog/');

$list = $dom->getElementsByTagName('title');
$title = $list->length ? $list->item(0)->textContent : '';
echo $title;

// and so on ...
Jan.J
  • 3,050
  • 1
  • 23
  • 33
1

You Should use Simple HTML Parser . And extract html using

$html = @file_get_html($url);
foreach($html->find('article') as element) {  
   $title = $dom->find('h2',0)->plaintext; 
   ....      
}

I am also using this, Hope it is working.

er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29