我一直在学习用 PHP 编程,我正在尝试使用 CURL 代替 file_get_contents($url) 制作 PHP 脚本
我想更多地了解 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);
但是,curl_exec();返回 1 并在我的页面上打印代码
有什么办法可以做我想做的事吗?
最佳答案
您需要使用:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
否则结果只是打印出来。
关于php 使用curl获取html并将其保存在txt中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20317265/