gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 14:03:08 26 4
gpt4 key购买 nike

我的网站有问题。我想从另一个网站获取所有计划航类数据。我看到了它的源代码并得到了处理数据的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com