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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19063745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:45:20  来源:igfitidea点击:

How to get data from url link to our website

phpschedule

提问by Agoeng Liu

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?

我的网站有问题。我想从另一个网站获取所有计划航班数据。我查看了它的源代码并获取了用于处理数据的 url 链接。有人能告诉我如何从当前 url 链接中获取数据,然后用 PHP 将其显示到我们的网站上吗?

回答by Sumit Bijvani

You can do it using file_get_contents()function. this function return html of provided url. then use HTML Parser to get required data.

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


Another way to extract data using preg_match_all()


使用提取数据的另一种方法 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>";
}

回答by Manish Goyal

Use file_get_contents

使用file_get_contents

Sample code

示例代码

<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

回答by user2801966

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

是的当然...使用 file_get_contents('$URl')函数来获取目标页面或使用卷曲的源代码,如果你喜欢使用curl ..和废钢你需要的所有数据preg_match_all()功能

Note : If the target url has https:// then you should use curl to get the source code

注意:如果目标 url 有 https:// 那么你应该使用 curl 来获取源代码

Example

例子

http://stackoverflow.com/questions/2838253/php-curl-preg-match-extract-text-from-xhtml

http://stackoverflow.com/questions/2838253/php-curl-preg-match-extract-text-from-xhtml