如何从网址链接到我们的网站获取数据

Ago*_*Liu 6 php schedule

我的网站有问题.我想从其他网站获取所有时间表的航班数据.我看到它的源代码并获取用于处理数据的url链接.有人可以告诉我如何从当前的url链接获取数据,然后用PHP将其显示在我们的网站上吗?

Sum*_*ani 6

你可以使用file_get_contents()功能.此函数返回提供的URL的html.然后使用HTML Parser获取所需数据.

$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
} 
Run Code Online (Sandbox Code Playgroud)


另一种使用提取数据的方法 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>";
}
Run Code Online (Sandbox Code Playgroud)