微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何从url链接到我们的网站获取数据

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

解决方法:

您可以使用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
} 

使用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>";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐