gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:43:32 24 4
gpt4 key购买 nike

我一直在学习用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com