Maison  >  Questions et réponses  >  le corps du texte

php如何从一个网址中获取到他所对应页面的html源代码

我用file_get_contents($url);这个方法不行
`$url = $_POST['url'];
$ch = curl_init();
$timeout = 3;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
exit;`
url是在前端输入的,我试过了,输入www.baidu.com,显示的仍是百度的界面,而不是他的html源代码

PHP中文网PHP中文网2720 Il y a quelques jours721

répondre à tous(4)je répondrai

  • 黄舟

    黄舟2017-04-11 10:18:46

    <?php
     $pagecontent = file_get_contents("http://www.w3cschool.cn");
     echo htmlspecialchars($pagecontent);//htmlentities 或者htmlspecialchars
     ?>
    

    这样就好了吧 你试试

    répondre
    0
  • 大家讲道理

    大家讲道理2017-04-11 10:18:46

    贴代码。你自己先在浏览器里访问下这个url

    répondre
    0
  • 迷茫

    迷茫2017-04-11 10:18:46

    如果你获取到了$contents,只是想在浏览器中显示源代码的话,可以用<pre>标签包裹一下显示

    répondre
    0
  • 天蓬老师

    天蓬老师2017-04-11 10:18:46

    $contents 就是对应 $url 的源码

    répondre
    0
  • Annulerrépondre