我想在我的网站上显示一个数据,该数据来自其他网站

时间:2016-03-28 14:10:58

标签: php html web

我对这个代码世界很陌生,但我真的很感兴趣。现在我遇到了问题。

我的问题从这里开始 -

这是一个例子。想象一下,我需要这个网站的数据[下图]

The Data I want form the website

您看到总点击次数:#Number#

现在,我希望在我的 index.html 中找到总点击次数:#Number#,这位于我的计算机中。如下所示[见图片]

What I really want

请告诉我是否有可能。如果是,请告诉我我应该使用的代码。

非常感谢..

2 个答案:

答案 0 :(得分:0)

是的,您可以使用cURL从其他网站获取数据,请参阅示例代码,我将获取另一个网站的第一个完整网页,之后我会找到H1 HTML文本的文本。

<?php
// Defining the basic cURL function
function curl($url) {
    $ch = curl_init();  // Initialising cURL
    curl_setopt($ch, CURLOPT_URL, $url);    // Setting cURL's URL option with the $url variable passed into the function
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
    $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
    curl_close($ch);    // Closing cURL
    return $data;   // Returning the data from the function
}
function getTextBetweenTags($string, $tagname) 
{
    $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
    preg_match($pattern, $string, $matches);
    return $matches[1];
}
$scraped_website = curl("http://www.example.com");
echo getTextBetweenTags($scraped_website,'h1');
?>

希望这对你有用。

答案 1 :(得分:0)

参考上述(显然是正确的......)答案,还要注意您的网站的最终用户在您的网络服务器收到之前不会收到任何HTML响应来自遥控器的响应。如果你可以合理地确信这会很快发生,那么没有问题。但是,如果可能需要一点时间,您可能希望做一些事情。 (用户因为&#34;触发快乐而臭名昭着。&#34;)

首先,你可以推动&#34;一些初始的HTML输出到用户的屏幕... dunno,一个小小的JavaScript进度条东西......既招待他又让他知道等待。然后,一旦获得该内容,请用真实回复替换该内容。

其次,只有如果安全性和其他因素允许这样的事情,您可以设计您的网站,以便客户通过AJAX请求附加信息 - 调用