使用Php获取网页内容

时间:2013-07-17 10:47:51

标签: php html

现在我知道这样做比较简单,但我想使用表单来获取要在其中使用的URL。我开始使用file_get_contents但后来切换到了fopen。

    <?php

  $marki = fopen($page, "rb");
  $page = $_GET['page'];


  echo $marki;

  ?>

<html>
  <body>
    <form method="get" action="index.php">
      <input type="text" name="page">
      <input type="submit" value="grade">
    </form>
  </body>
</html>

这是我到目前为止使用的代码,我认为答案仍然可以使用file_get_contents,但它不会获取页面内容,因为它一直将获取内容指向存储在我的服务器中的文件,这不是我的意思瞄准。

3 个答案:

答案 0 :(得分:2)

$page之前设置$marki,例如 -

$page = $_GET['page'];
$marki = fopen($page, "rb");

答案 1 :(得分:0)

当您使用'file_get_contents'时,您是否在网址前加了“http://”?如果您没有,它将尝试在您自己的服务器上找到该URL。见下文:

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

官方文档PHP:file_get_contents

答案 2 :(得分:0)

您也可以尝试cURL。我个人刚刚学会了如何做到这一点,它比file_get_contents快了大约6倍。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_HTTPGET, TRUE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);
?>

当你echo $output时,它会返回它所获得的页面。也可以用它做更多的事情。