Php 如何使用get方法从url获取html代码?
我想从页面获取html代码。使用方法get生成的页面和文件get内容不起作用。您可以更具体一点,还是发布您的代码Php 如何使用get方法从url获取html代码?,php,html,Php,Html,我想从页面获取html代码。使用方法get生成的页面和文件get内容不起作用。您可以更具体一点,还是发布您的代码 <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> </head> <body> <div id="content"></div&
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<div id="content"></div>
<script>
/**
* Test harness to find out the best method for dynamically loading a
* html page into your app.
*/
var test_times = {};
// here you can use your page name to load
var test_page = 'testpage.htm';
var content_div = document.getElementById('content');
// TEST 1 = use jQuery to load in testpage.htm and time it.
/*
function test_()
{
var start = new Date().getTime();
$(content_div).load(test_page, function() {
alert(new Date().getTime() - start);
});
}
// 1044
*/
// TEST 2 = use <object> to load in testpage.htm and time it.
/*
function test_()
{
start = new Date().getTime();
content_div.innerHTML = '<object type="text/html" data="' + test_page +
'" onload="alert(new Date().getTime() - start)"></object>'
}
//1579
*/
// TEST 3 = use httpObject to load in testpage.htm and time it.
function test_()
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
content_div.innerHTML = xmlHttp.responseText;
alert(new Date().getTime() - start);
}
};
start = new Date().getTime();
xmlHttp.open("GET", test_page, true); // true for asynchronous
xmlHttp.send(null);
// 1039
}
// Main - run tests
test_();
</script>
</body>
</html>
我试过这样的方法,效果很好:
<?php
$html=file_get_contents("http://www.google.com");
echo $html;
?>
要使用get方法,您可以将get参数添加到您正在调用的url,如下所示:
<?php
$html=file_get_contents("http://yoururl.com?param1=value¶m2=value");
echo $html;
?>
你能告诉我们一些错误吗?然后我尝试一些类似$html=file\u get\u contents;函数不工作并显示消息:无法打开流:没有这样的文件或目录。但是我尝试了一些类似$html=file\u get\u contents的东西;它可以工作。错误意味着它无法访问文件。请检查您指定的url是否有效。您必须在代码中指定http://。我在我的一台服务器上制作了一个php文件来尝试这一点,它可以正常工作