如何使用get方法从url中获取html代码


How to get html code from url using method get?

我想从页面中获取html代码。使用get和file_get_contents()方法生成的页面不起作用。

您可以更具体一点还是发布您的代码?

我试过这样的东西,它有效:

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

要使用get方法,您可以将get参数添加到您调用的url中,如下所示:

<?php
$html=file_get_contents("http://yoururl.com?param1=value&param2=value");
echo $html;
?>
<!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>