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

Vít*_*peã 2 php curl

我一直在学习用PHP编程,我正在尝试使用CURL编写PHP脚本而不是file_get_contents($ url)

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

所以,我编码这个从一些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);
Run Code Online (Sandbox Code Playgroud)

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

无论如何要做我想要的?

Koh*_*ese 5

你需要使用:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
Run Code Online (Sandbox Code Playgroud)

否则结果只是打印出来.