php使用curl获取html并将其保存在txt中


php using curl to get the html and save it on a txt

我一直在学习用PHP编程,我正试图用CURL代替file_get_contents($url)制作一个PHP脚本

我想了解更多关于卷曲的原因是因为它更快、更灵活,但更难

所以,我编写这个代码是为了从某个URL下载HTML代码,并将其保存在TXT 上

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.pt");
curl_setopt($ch, CURLOPT_HEADER, 0);
//should save html in $html
$html = curl_exec($ch);
//save html on txt
$fh = fopen('html.txt', 'w') or die("can't open file");
        fwrite($fh, $html);
        fclose($fh);
curl_close($ch);

但是,curl_exec();返回1并在我的页面上打印代码

有没有办法做我想做的事?

您需要使用:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );

否则,结果只会打印出来。