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?
Asked
Active
Viewed 3.8k times
5
-
Sounds like scraping. What's the link, and is there an API that you could be using? – Dave Chen Sep 28 '13 at 05:46
-
it is scraping. you want [DOMDocument](http://php.net/manual/en/class.domdocument.php) and [XPath](http://php.net/manual/en/class.domxpath.php). Hopefully you have asked permission of the other website – gwillie Sep 28 '13 at 06:38
3 Answers
6
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>";
}

Sumit Bijvani
- 8,154
- 17
- 50
- 82
1
Sample code
<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

Manish Goyal
- 700
- 1
- 7
- 17
0
Yes Sure ... Use file_get_contents('$URl')
function to get the source code of the target page or use curl if you prefer using curl .. and scrap all data you need with preg_match_all()
function
Note : If the target url has https:// then you should use curl to get the source code
Example
http://stackoverflow.com/questions/2838253/php-curl-preg-match-extract-text-from-xhtml

user2801966
- 418
- 3
- 11