你应该看看jQuery。它具有丰富的AJAX 功能基础,可以让您完成所有这些工作。您可以加载外部页面,并使用直观的类似 CSS 的选择器解析其 HTML 内容。
使用示例$.get();
$.get("anotherPage.html", {}, function(results){
alert(results); // will show the HTML from anotherPage.html
alert($(results).find("div.scores").html()); // show "scores" div in results
});
对于外部域,我不得不编写一个充当中间人的本地 PHP 脚本。jQuery 将调用本地 PHP 脚本,传入另一个服务器的 URL 作为参数,本地 PHP 脚本将收集数据,jQuery 将从本地 PHP 脚本中读取数据。
$.get("middleman.php", {"site":"http://www.google.com"}, function(results){
alert(results); // middleman gives Google's HTML to jQuery
});
给middleman.php一些类似的东西
<?php
// Do not use as-is, this is only an example.
// $_GET["site"] set by jQuery as "http://www.google.com"
print file_get_contents($_GET["site"]);
?>