作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的网站有问题。我想从另一个网站获取所有计划航类数据。我看到了它的源代码并得到了处理数据的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>";
}
关于php - 如何从 url 链接到我们的网站获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063745/
我是一名优秀的程序员,十分优秀!