How to get data from url link to our website

Agoeng Liu picture Agoeng Liu · Sep 28, 2013 · Viewed 35.4k times · Source

I have a problem with my website. I want to get all the schedule flight data from another website. I see its source code and get the url link for processing data. Can somebody tell me how to get the data from the current url link, then display it to our website with PHP?

Answer

Sumit Bijvani picture Sumit Bijvani · Sep 28, 2013

You can do it using file_get_contents() function. this function return html of provided url. then use HTML Parser to get required data.

$html = file_get_contents("http://website.com");

$dom = new DOMDocument();
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('h3');
foreach ($nodes as $node) {
    echo $node->nodeValue."<br>"; // return <h3> tag data
} 


Another way to extract data using preg_match_all()

$html = file_get_contents($_REQUEST['url']);

preg_match_all('/<div class="swrapper">(.*?)<\/div>/s', $html, $matches);
   // specify the class to get the data of that class
foreach ($matches[1] as $node) {
    echo $node."<br><br><br>";
}